Whale Talk step 9

Hi, I just finished the Whale Talk project. I finished with help from the developer in the video. I do understand my code and it’s working correctly.

I just don’t get step 9.

Step 9 says:

Whales double their e ‘s and the u ‘s in their language.

Write an if statement that checks if each letter in the input string is equal to 'e' . If so, .push() input[i] to the resultArray .

Note, this statement belongs after the inner for loop block inside the outer for loop. This is because you only want to perform this check once for every letter in the input .

The hint says:

To push the e ‘s a second time create a conditional like this:

if (stringName[i] === 'e'){
  myArray.push(stringName[i]);
}

But this does not work.
Why do you need to put the if/else statement in the outer loop?
The developer in the video wrote the if/else statement in the inner loop. That works wel.
Because in my view: you have to do something with the comparison.

Can someone explain this to me?

Here is my code:

const input = 'i love to learn to code at codecademy and so do you';
const vowels = ['a', 'e', 'i', 'o', 'u'];
let resultArray = [];
for(let i = 0; i < input.length; i++) {
  // console.log('i is ' + i);
  for(let j = 0; j < vowels.length; j++) {
    if(input[i] === vowels[j]) {
      if(input[i] === 'e') {
        resultArray.push('ee');
      } else if(input[i] === 'u') {
        resultArray.push('uu');
      } else {
        resultArray.push(input[i]);
        }
      } 
    }
  }      
console.log(resultArray.join(' ').toUpperCase());
// prints  I O EE O EE A O O EE A O EE A EE A O O O UU

a link to the exercise: https://www.codecademy.com/paths/front-end-engineer-career-path/tracks/fecp-javascript-syntax-part-ii/modules/fecp-learn-javascript-syntax-loops/projects/whale-talk

Thank you in advance for your help.

Hi @rulesbyrosita
actually it does work:

const input = 'i love to learn to code at codecademy and so do you';
const vowels = ['a', 'e', 'i', 'o', 'u'];
let resultArray = [];
for(let i = 0; i < input.length; i++) {
  // console.log('i is ' + 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]);
  }
}
console.log(resultArray.join(' ').toUpperCase());

What did you do differently?

1 Like

Hi Thanks! I put all the if statements in the inner loop, I see now what Codecademy tells at step 9. But I have still one question How does the code know that ‘e’ has to turn in ‘ee’ and ‘u’ in ‘uu’ ?

Because you push the 'e’s twice. Inside the inner for loop you push each vowel to the array once. Including the 'e’s. And below the inner for loop, you just push the 'e’s into the loop for a second time.

const input = 'i love to learn to code at codecademy and so do you';
const vowels = ['a', 'e', 'i', 'o', 'u'];
let resultArray = [];
for(let i = 0; i < input.length; i++) {  // here you iterate through each letter in your sentence (input)
  for(let j = 0; j < vowels.length; j++) { // for each letter in your sentence you iterate through each item in your array (vowels)
    if(input[i] === vowels[j]) {  // let's play it for the first iteration of the outer loop: i == 0, input[i] == 'i'. So you compare 'i' with each vowel
      resultArray.push(input[i]); // the third iteration (starting from 0) in your inner loop is a hit: vowels[2] == 'i'
    }
  }  
  if( input[i] === 'e'){ // here you don't need to iterate through the array vowels, because just want to find 'e'
  	resultArray.push(input[i]); 
  }
}
1 Like

Thank you! Now I fully understand my code and the project! And Thanks for the explanation.
Here is the finished project:

const input = 'i love to learn to code at codecademy and so do you';
const vowels = ['a', 'e', 'i', 'o', 'u'];
let resultArray = [];
for(let i = 0; i < input.length; i++) {
  // console.log('i is ' + 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]);
  } else if(input[i] === 'u') {
    resultArray.push(input[i]);
  }
}
console.log(resultArray.join(' ').toUpperCase());
// prints: I O E E O E E A O O E E A O E E A E E A O O O U U
1 Like

I completed this project recently with the below method. I found using a nested if function to check if the vowel is a U or E (after determining that it is, in fact, a vowel) to be an easy solution. From there, if it is a U or E, the result just gets pushed twice, else if it is any other vowel, it only gets pushed once.

Also if you struggle with the nested loops, you can un-comment my console.log() lines near the bottom and it will output the iterators at every loop, which I found to be a handy way of visualizing how the computer is running through the 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 v = 0; v < vowels.length; v++) {
    if (input[i].toUpperCase() === vowels[v]) {
      if (input[i].toUpperCase() === 'U' || input[i] === 'E') {
        resultArray.push(input[i].toUpperCase(), input[i].toUpperCase());
      } else resultArray.push(input[i].toUpperCase());
    }
     //console.log(i, v); //prints current i and v for each iteration of inside loop
  } 
  //console.log(i); //prints i for each iteration of outside loop. these logs provide a good visual for how the loops iterate through the code (or how the code iterates throug the loops?)
}

console.log(resultArray.join('')); //outputs UUEIEAUUE

Happy coding!