I am having trouble figuring out how to use an if statement that checks if a letter in a string is equal to e or u and then replacing it with ee or uu. The code I have works, but doesn’t seem like the simplest way to do it.
Link to project
Here is my code:
const vowels = ['a', 'e', 'i', 'o', 'u'];
let resultArray = [];
for (let i = 0; i < input.length; i++) {
for (let v = 0; v < vowels.length; v++) {
if (input[i] === vowels[v]) {
if (input[i] === 'u' || input[i] === 'e') {
resultArray.push(input[i], input[i]); //non-repetitive way
} else { //to do this?
resultArray.push(input[i]);
}
}
}
};
console.log(resultArray.join('').toUpperCase()); //output: UUEEIEEAUUEE
You can split it into smaller parts and use map and filter
One useful component would be isVowel, another would be one that doubles only e/u while letting others through without modification
> isVowel = (`elem` "aoeui")
> doubleEU = (\c -> if c `elem` "eu" then [c,c] else [c])
> "blahblehbluh" & map doubleEU >>= filter isVowel
"aeeuu"
Each individual part is simpler because it’s doing fewer things. Combining them still gives the same outcome.
A less drastic change is to do one thing and then the other, instead of doing them both at the same time. You could filter out non-vowels. Then, modify the e’s and u’s.
// not nested:
for whatever {
eliminate non-vowels
}
for something {
alter e's and u's
}
You could add an additional variable which has the conversion vowels pre-made so instead of pushing the input[i] value you could instead push the conv[v] value. Something like this:
let input = 'turpentine and turtles'
const vowels = ['a', 'e', 'i', 'o', 'u'];
const conv = ['A', 'EE', 'I', 'O', 'UU']
let resultString = '';
for (let i = 0; i < input.length; i++) {
for (let v = 0; v < vowels.length; v++) {
if (input[i] === vowels[v]) {
resultString += conv[v] //non-repetitive way
}
}
}
console.log(resultString); //output: UUEEIEEAUUEE