Skip to content

Instantly share code, notes, and snippets.

@jloescher
Created February 7, 2021 02:00
Show Gist options
  • Save jloescher/62c055ad52b1a073fdab7e69fcc8b67f to your computer and use it in GitHub Desktop.
Save jloescher/62c055ad52b1a073fdab7e69fcc8b67f to your computer and use it in GitHub Desktop.
Convert a phrase to whale talk using function, loops, and arrays.
let input = 'Anh yeu em, yeu hon!';
const vowels = ['a', 'e', 'i', 'o', 'u'];
resultsArray = [];
function whaleTalk(phrase) {
const lowerCasePhrase = phrase.toLowerCase();
for (let l = 0; l < lowerCasePhrase.length; l++) {
// console.log(lowerCasePhrase[l]);
for (let v = 0; v < vowels.length; v++) {
if (vowels[v] === lowerCasePhrase[l]) { // whales only use vowels.
if (lowerCasePhrase[l] === 'e' || lowerCasePhrase[l] === 'u') { // e's and u's are doubled in whale talk.
resultsArray.push(lowerCasePhrase[l], lowerCasePhrase[l]);
} else { // all remaining vowels are singular.
resultsArray.push(lowerCasePhrase[l]);
}
}
}
}
console.log(resultsArray.join(''));
}
whaleTalk(input);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment