JavaScript Project Help: Whale Talk

Hi Everyone!

I am driving myself crazy as I cannot get my output to equal the test …

It is the whale project:
https://www.codecademy.com/courses/introduction-to-javascript/projects/whale-talk

This is my code:

let input = 'turpentine and turtles';

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

let resultArray = [];

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

console.log(resultArray.join('').toUpperCase());

And the output is: UUUUUEEEEEEEEEEUUUUUEEEEE

But the test says it should be: UUEEIEEAUUEE.

Can someone please help me as I am stuck!?

The loops are more complicated than needs be since we do not need the indices, only the letters.

for (let letter of input) {
  for (let vowel of vowels) {
    if (letter === vowel) {
      result.push(letter)
    }
    if (letter === 'e' or letter === 'u') {
      result.push(letter)
    }
  }
}

In the above we push all vowels at least once, and again when it is ‘e’ or ‘u’.

1 Like

You have a couple of problems:

  1. You aren’t validating anything against the values in vowels[]. a, i, and o are going undetected because no code checks if input[i] === vowels[j] and pushes input[i] when the check passes.

  2. Every time it finds a u or an e, that letter is pushed to resultArray[] five times. This is once for every letter in the vowels[] array, because you are only checking if (input[i] === 'e' || input[i] === 'u'), and you do that for every letter in vowels[], instead of only when input[i] === vowels[j].

Let 's run through the loop and see how it is currently working:

  1. On input[0] === 't', as you iterate from ‘a’, vowels[0] through ‘u’, vowels[4], ('t' === 'e' || 't' === 'u' returns false all five times and nothing is pushed to the array. T doesn’t belong in the array, so no problem.

  2. On input[1] === 'u', as you iterate through vowels[0] through vowels[4], ('u' === 'e' || 'u' === 'u' returns true all five times and u is pushed to the array all five times. U does belong in the array, but only twice in a row.

  3. Let’s skip to input[7] === 'i'. As you iterate through vowels[0] through vowels[4], ('i' === 'e' || 'i' === 'u' returns false all five times so nothing is pushed to the array. However, i belongs in the array.

Here’s how we’d fix your for loop WITHOUT refactoring it with for...of:

for (let i = 0; i < input.length; i++) {
  for (let j = 0; j < vowels.length; j++) {
    //ADD this to check if input is in vowels
    if (input[i] === vowels[j]) {
      //PUSH input[i] if it's in vowels
      resultArray.push(input[i]);
      // NEST this so you only append the second e/u once, after finding it in vowels
      if (input[i] === 'e' || input[i] === 'u') {
        resultArray.push(input[i]); // good practice to add semi-colons
      }
    }
  }
};

mtf’s version is better optimized, but I don’t think for...of was covered yet, and even if it was it’s good to know why code worked unexpectedly.

1 Like

Thank you so much for the explanation! :slight_smile:

I used this as this was how I understood the instructions. But yes the MTF version also worked :slight_smile:

1 Like