Whale Talk exercise

Hello! I have been having trouble with the output for this exercise. when I complete the log at the end it is doubling the result. When I enter the sample input “Hi, Human” it should log IUUA but my result is IIUUUAA. I think I made an error in how I nested the loops but I can’t seem to match it with the video. I would appreciate the feedback from anyone who has the time take a look!

const input = "Hi, Human"
const vowels = ["a", "e", "i", "o", "u"]
const resultArray = []
for (let inputIndex = 0; inputIndex < input.length; inputIndex++){
for (let vowelIndex = 0; vowelIndex < vowels.length; vowelIndex++){
 if (input[inputIndex] === vowels[vowelIndex]){
  if (input[inputIndex] === 'e'){
   resultArray.push('ee');
  }
   else {
     resultArray.push(input[inputIndex]);
     if (input[inputIndex] === 'u'){
   resultArray.push('uu');
  }
   else {
     resultArray.push(input[inputIndex]);
    }
   }
  }
 }
}
console.log(resultArray.join('').toUpperCase());

Hey @core1130316899, welcome to the forums!

Could you please post the link to the exercise?

Hey @core1130316899,

The cause lies indeed in your for loops.

if (input[inputIndex] === 'e'){
   resultArray.push('ee');
  }
   else {
     resultArray.push(input[inputIndex]);

This code above causes the else condition to run when input[inputIndex] === 'u' and pushes the 'u' to the resultArray. Immediately after that this code:

if (input[inputIndex] === 'u'){
   resultArray.push('uu');
  }
  else {
     resultArray.push(input[inputIndex]);
   }

Causes 'uu' to be pushed as well. Therefore you have triple u’s.

This is also why you have double I’s and A’s and should get double O’s. The first if condition causes the else to kick, pushes the value to resultArray, then with the last else condition it is being pushed again.

The way to solve this is to make it so that you perform resultArray.push(input[inputIndex]); only once.

Hope this guides you in the correct direction. If you can’t get it, don’t hesitate to ask again.

Happy coding!

PS: there is also the else if condition, I suggest finding an application for that ;).

@stevencopeland https://www.codecademy.com/paths/web-development/tracks/web-dev-js-arrays-loops-objects/modules/learn-javascript-loops/projects/whale-talk

2 Likes

Thank you so much! After your explanation I realized that I had completely spaced that this would be an if, else if, else type situation! I had been lazy and had copy/pasted the first if input index for e and u