____________________________________________________________________________________________________________________________________________






____________________________________________________________________________________________________________________________________________


Measuring String Length

  • The len() function in Python allows us to find the length of a string
  • Use '.length' in JavaScript to do this as well

  • Sample Hack - String Analyzation

    • Determine metrics about strings (length, chars, palidrome(?), etc.)
    %%js
    const string1 = "Brawl Stars is a fun game!";
    const string2 = "Star Brawl war Brats";
    
    
    console.log("String 1: " + string1);
    const length1 = string1.length;
    console.log("Length: " + length1);
    
    
    function countVowels(inputString) {
        const vowels = 'aeiouAEIOU';
        let count = 0;
        for (const char of inputString) {
            if (vowels.includes(char)) {
                count++;
            }
        }
        return count;
    }
    
    
    console.log("Vowel Count: " + countVowels(string1));
    
    
    function averageWordLength(inputString) {
        const words = inputString.split(/\s+/);
        if (words.length === 0) {
            return 0;
        }
        const totalLength = words.reduce((sum, word) => sum + word.length, 0);
        return totalLength / words.length;
    }
    
    
    console.log("Average Word Length: " + averageWordLength(string1));
    
    
    function isPalindrome(inputString) {
        const sanitizedString = inputString.replace(/\s+/g, '').toLowerCase();
        return sanitizedString === sanitizedString.split('').reverse().join('');
    }
    
    
    console.log("Palindrome or Not? " + isPalindrome(string1));
    console.log("String 2: " + string2);
    const length2 = string2.length;
    console.log("Length: " + length2);
    console.log("Vowel Count: " + countVowels(string2));
    console.log("Average Word Length: " + averageWordLength(string2));
    console.log("Palindrome or Not? " + isPalindrome(string2));
    
    <IPython.core.display.Javascript object>
    

    Homework is to create a JavaScript hack for Password Validator