Created
March 8, 2021 16:22
-
-
Save alexcrist/39903846f480cbd47bba89a997b59a1f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function onlyVowels(str) { | |
let output = ''; | |
for (const char of str) { | |
const lowerCaseChar = char.toLowerCase(); | |
if ( | |
lowerCaseChar === 'a' || | |
lowerCaseChar === 'e' || | |
lowerCaseChar === 'o' || | |
lowerCaseChar === 'u' || | |
lowerCaseChar === 'i' | |
) { | |
output += char; | |
} | |
} | |
return output; | |
} | |
function crazyCase(str) { | |
let output = ''; | |
for (let i = 0; i < str.length; i++) { | |
const char = str[i]; | |
// If index is even? Lower case the char | |
if (i % 2 === 0) { | |
output += char.toLowerCase(); | |
} | |
// Is the index odd? Upper case the char | |
else { | |
output += char.toUpperCase(); | |
} | |
} | |
return output; | |
} | |
function titleCase(str) { | |
let output = ''; | |
let shouldCapitalizeNextLetter = true; | |
for (const char of str) { | |
// We should capitalize the char | |
if (shouldCapitalizeNextLetter) { | |
output += char.toUpperCase(); | |
} | |
// We should lower case the char | |
else { | |
output += char.toLowerCase(); | |
} | |
// Determine whether we should capitalize the next letter | |
if (char === ' ') { | |
shouldCapitalizeNextLetter = true; | |
} else { | |
shouldCapitalizeNextLetter = false; | |
} | |
// Simplification of above if statement: | |
// shouldCapitalizeNextLetter = char === ' '; | |
} | |
return output; | |
} | |
function camelCase(str) { | |
let output = ''; | |
let shouldCapitalizeNextLetter = false; | |
for (const char of str) { | |
// Add character to output string, unless its a space | |
if (char !== ' ') { | |
// If 'shouldCapitalizeNextLetter is true, capitalize | |
// Otherwise, lowercase the char | |
if (shouldCapitalizeNextLetter) { | |
output += char.toUpperCase(); | |
} else { | |
output += char.toLowerCase(); | |
} | |
} | |
// If the character is a space, capitalize the next letter | |
// Otherwise, lower case the next letter | |
if (char === ' ') { | |
shouldCapitalizeNextLetter = true; | |
} else { | |
shouldCapitalizeNextLetter = false; | |
} | |
} | |
return output; | |
} | |
function crazyCase2ReturnOfCrazyCase(str) { | |
let output = ''; | |
let shouldCapitalizeNextLetter = false; | |
for (const char of str) { | |
// Add current char to output string, capitalize or lowercase as appropriate | |
if (shouldCapitalizeNextLetter) { | |
output += char.toUpperCase(); | |
} else { | |
output += char.toLowerCase(); | |
} | |
// Toggle `shouldCapitalizeNextLetter` if the current char is not a space | |
if (char !== ' ') { | |
shouldCapitalizeNextLetter = !shouldCapitalizeNextLetter; | |
} | |
} | |
return output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment