Whale Talk : double every vowel

Hello, everyone.
I just finished the Whale Talk exercice but i have a small issue : instead of only double “e” and “u”, the output doubles every vowel (‘UUEEIIEEAAUUEE’ instead of ‘UUEEIEEAUUEE’).
I can’t find where it comes from, thanks for your helping.

Here’s my code:

const input = 'turpentine and turtles';
const vowels = ['a', 'e', 'i', 'o', 'u'];
let resultArray = [];
for(let i = 0; i < input.length; i++) {
  for(let vowel = 0; vowel < vowels.length; vowel++) {
    if(input[i] === vowels[vowel]) { 
  if(input[i] === 'e') {
    resultArray.push('e')
  } else {
   resultArray.push(input[i]);
 }
 if(input[i] === 'u') {
   resultArray.push('u')
 } else {
   resultArray.push(input[i]);
    };
   };
  };
};

  console.log(resultArray.join('').toUpperCase());

Oh yeah, the same thing happened to me while making this project :sweat_smile:

Let’s break down your if statements:

First, we check if the current letter of the input is a vowel. Nice.

Now, if it is a vowel, we check if it’s an ‘e’. If it is, we push that ‘e’ to the array.
If it’s anything else than an ‘e’, we push whatever that vowel is to the array. (This one could be either a ‘u’ or any other vowel except from ‘e’)

Now we’re making another check. This one is a whole other if/else block.
If the vowel is a ‘u’, push it into the array. Now, if in your previous if/else block you caught a ‘u’, here’s where you’d double it.
If the vowel is anything else than ‘u’ (either ‘e’ or any other vowel that’s not ‘u’), it will also be pushed into the array.
Now what if you had pushed a vowel that wasn’t neither ‘e’ or ‘u’ in your first else statement? You would push it yet again in this else statement.
The vowels that aren’t ‘e’ or ‘u’ are being pushed to the array in two places in your code.

I suggest you rewrite your logic by including an else if statement.
I hope this helped you, please tell me if you have any questions! :smiley_cat:

3 Likes

Hello, yizuhi.

I have found my mistake, thank to your help.

Thanks a lot.

Provenance : Courrier pour Windows 10

1 Like

Yay! Glad I could help :blush:

1 Like