Whale Project - vowels not double

Hi everybody,
I’m at the Whale Projet in Javascript and cannot figure out why my vowels are not double (‘e’ should be pushed to ‘ee’, ‘u’ to ‘uu’)

let input = "turpentine and turtles";

const vowels = ['a', 'e', 'i', 'o', 'u'];
let resultArray = [];


for (let i = 0; i < input.length; i++){
 // console.log(i);
 for (let j = 0; j < vowels.length; j++){
 // console.log(j)
 if (input[i] === vowels[j]) {
    resultArray.push(input[i]);
    }
 if (input[i] === ['e'] || input[i] === ['u']) { 
   resultArray.push(input[i]);
    }
 }
}
console.log(resultArray.join('').toUpperCase());

Try printing input[i] right before this line to see what you are comparing.

Hint:

Is 'e' the same as ['e']?

I may be very stupid. Thanks so much!

Now it prints the ‘e’ and the ‘u’ too many times however…

if (input[i] === 'e' || input[i] === 'u') { 
   resultArray.push(input[i]);
    }
 }
}
console.log(resultArray.join('').toUpperCase());

prints UUUUUUEEEEEEIEEEEEEAUUUUUUEEEEEE

I think it’s because i’m pushing (input[i] twice (see above)? But if I don’t do that, all vowels apart from e and u disappear.

You’re dealing with loops, and you’re getting too many of something. That would be an indication that something is inside a loop that maybe shouldn’t be. Consider carefully the order of execution of each line of your code. Particularly this line:

How many times is this condition checked, and a value pushed to your resultArray?

Oh my god. THANK YOU. it helps so much to ask stupid questions and getting nudged in the right direction!

Don’t beat yourself up. You’re not stupid, and you haven’t asked stupid questions. Keep up the good work, and happy coding!

1 Like

Hi Midlindner or Eagernoob, I tried a similar approach when writing the “doubling” of the letters but mine did not work and I am unclear as to why.

Ive been searching for an answer by reading up on “if” and || but I just don’t understand.

I originally wrote :
if (input[i]=== ‘a’||‘o’||‘e’) {
resultArray.push(input[i])
}
//for some reason the || statement is duplicating ALL letters not just the indicated ones.

//After reading I found a solution to the problem but no explanation.

if (input[i]=== ‘a’ || input[i]=== ‘o’ || input[i]=== ‘e’){
resultArray.push(input[i])
}

This works I just don’t understand why.

If you want to make multiple comparisons, you have to use the syntax from your example that works. When you use your original syntax, what happens is:

  1. input[i] === ‘a’ is evaluated. If that is truthy the condition is met, and the code inside the if block is executed.

  2. If the above is falsy, 'o' is evaluated for truthiness. Since any non-empty string has a boolean value of true, the condition is met, and the code inside the if block is executed.

The evaluation ends there. The || 'e' portion is never reached since 'o' is truthy. In an OR || comparison, the first truthy value (from left to right) is returned, so that particular if condition will always evaluate to truthy.

Thank you so much for your quick reply.

Just to confirm I understand…im going to add a few more parenthesis

(input[i]=== ‘a’||‘o’||‘e’) is being read by the computer as ( (input[i]=== ‘a’) ||‘o’||‘e’);
where the first statement (input[i]=== ‘a’) is actually comparing the array contents to the given letter ‘a’,
but the second statement following || is not being compared to the array at all,
only being taken at face value as a non-empty string?
long run on question… but I think it got my questions across.

lol
Thanks again.

1 Like

Your understanding appears to be correct. :slightly_smiling_face:

1 Like