Fall 2024 - P1
Big Idea 3 | .1 | .2 | .3 | .4 | .5 | .6 | .7 | .8 | .10 |
3.4 String Operations
3.4 Team Teach String Operations
- Measuring String Length
- Sample Hack - String Analyzation
- Homework is to create a JavaScript hack for Password Validator
____________________________________________________________________________________________________________________________________________
____________________________________________________________________________________________________________________________________________
Measuring String Length
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>