Whale talk - step 7

Hi, I am working through loops in JavaScript right now with the project Whale Talk. My code works but there is one part don’t understand why it behaves the way that it does. Here is my code:

https://www.codecademy.com/workspaces/63d2e321206871a64dd1ed93

The code I’m a bit confused about is from step 7 in the project:

  console.log(`vowelIndex is ${vowelIndex}`)
  if (input[inputIndex] === vowels[vowelIndex]) {
    //console.log(input[inputIndex])
    resultArray.push(input[inputIndex]);
  }
}
}

When printing the vowelIndex to the console (I will comment this out later), numbers 0-4 are printed repeatedly. I know it says in step 7 that this will happen, but I don’t understand why it prints so many times. Why doesn’t it go though the numbers just once?

Also, please give me any other feedback on the code if you want to!

It was hard to tell because your indenting was off, but your second for-loop is nested inside your first. That means that for every inputIndex value, you run the entirety of the vowelIndex loop.
To see why this is, try inserting this into your code right before the vowelIndex log:

console.log(`inputIndex is ${inputIndex}`)

You’ll find that for every inputIndex log you’ll get a number of vowelIndex logs equal to the length of your vowel list.

2 Likes

Thank you so much for explaining it to me!