Skip to content

Instantly share code, notes, and snippets.

@ChousinRahit
Last active September 3, 2023 11:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChousinRahit/052546ed0a9c82bbe2967dca724effda to your computer and use it in GitHub Desktop.
Save ChousinRahit/052546ed0a9c82bbe2967dca724effda to your computer and use it in GitHub Desktop.
// Extend the Vowels
// Create a function that takes a word and extends all vowels by a number num.
// extendVowels("Hello", 5) ➞ "Heeeeeelloooooo"
// extendVowels("Edabit", 3) ➞ "EEEEdaaaabiiiit"
// extendVowels("Extend", 0) ➞ "Invalid"
// Notes
// Return "invalid" if num is not a positive integer or 0.
// Pseudocode
// write a function "extendVowels" function takes a string and a number
// take a variable to store result string "resultStr"
// convert string to array and loop through
// if vowel is found concat to "resultStr" by repeating same letter number of times given in input plus one
// if not just concat to "resultStr" once
// return resultStr
let isVowel = char => ['a', 'e', 'i', 'o', 'u'].includes(char.toLowerCase());
function extendVowels(str, num) {
if (num < 1) return 'invalid';
let resultStr = '';
[...str].forEach(char => {
if (isVowel(char)) {
resultStr = resultStr + char + char.repeat(num);
} else {
resultStr = resultStr + char;
}
});
return resultStr;
}
console.log(extendVowels('Hello', 5));
console.log(extendVowels('Edabit', 3));
console.log(extendVowels('Extend', 0));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment