Whale Talk - logging numbers?

Hi everyone!

I just finished Whale Talk. My code is listed below. This works perfectly fine (though, please, if there is a way I can improve it let me know!) Initially, though, I had resultArray.push(i), which was returning numbers interpolated in the strings. When I changed it to resultArray.push(input[i]), it stopped doing that. I can’t figure out why it would push numbers to begin with, though. Any insight?

Thanks again!

`let input = ‘Hi, Human’;
const vowels = [‘a’,‘e’,‘i’,‘o’,‘u’];
const resultArray = ;

for (let i = 0; i <= input.length; i++) {
for (let j = 0; j < vowels.length; j++) {
if (input[i] === vowels[j]) {
resultArray.push(input[i]);
}
} if (input[i] === ‘e’) {
resultArray.push(input[i]);
} if (input[i] === ‘u’) {
resultArray.push(input[i]);
}
}

console.log(resultArray.join(’’));`

Cheers!
—Codecademy Community Managers

When you did resultArray.push(i) you were pushing the variable that you used in your for-loop (starting with 0 and counting up to the length of the string). So you were getting numbers in the resultArray for that reason.

You could also have used the array method .includes which checks if an array includes a certain value (letter in this case) was includes in vowels. That would have allowed you to avoid the nested inner for loop. Also the || (or) operator could be used to check for several conditions (e and u) in one line of code.

1 Like

Thank you! I think that makes sense - resultArray.push was pushing each step instead of just the letter comparison step?

Thanks for the tips to pare things down. I’ll head back and play with those things to get used to them. I forget about the || operator a lot…practice, practice, practice!