Whale Talk. Why this doesn't work properly?

Hi everybody!

I have a problem with ‘Whale Talk’ exercise! Below is my code!

let input = '' let vowels = ['a', 'e', 'i', 'o', 'u'] let resultArray = [] let i let v input = 'Hi, human' for(i = 0; i < input.length; i++){ for(v = 0; v < vowels.length; v++){ if(input[i] === vowels[v]){ resultArray.push(input[i]) } if(input[i] === 'e' || input[i] === 'u'){ resultArray.push(input[i]) } } } console.log(resultArray.join('').toUpperCase())

Correct answer = IUUA

To be honest, i don’t know why my code repeat too many vowels. In my head, my code should print one vowels if “(input[i] === vowels[v])” and, another one if it is ‘e’ or ‘u’.

PD: Later, I can solve properly this exercise but i want to kwon what was wrong with mine code.

Thank you so much! Have a nice day!

Hello @erodriguez10, welcome to the forums! As the code currently is, it will add a U for every element in the vowels array. Notice how this block

if(input[i] === 'e' || input[i] === 'u'){
      resultArray.push(input[i])
    }

Is within the for(v = 0; v < vowels.length; v++) block?

1 Like

Hi @codeneutrino !

Thanks you for the answer. It is not at all clear my dude but at least, I know why it will add a lot of U.

Have a nice day! :slight_smile:

1 Like

I made the same mistake when I did that exercise.
That last if statement needs to be outside the inner for loop (but inside the outer for loop).

1 Like

Hi @janbazant1107978602 !

Thank you so much your time. I test it and that was the real solution. Now, that make sense. One vowels if it is the same in input and vowels, and one more vowel if it is “e” or “u”.

Have a nice day!