Last active
September 3, 2023 11:00
-
-
Save ChousinRahit/052546ed0a9c82bbe2967dca724effda 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
// 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