Whale Talk: doulbe e‘s and u's

I have a difficulty with the " Whale Talk" exercise to practice loops.
The link to the exercise: https://www.codecademy.com/courses/introduction-to-javascript/projects/whale-talk?action=resume_content_item

I suppose the issue that the code doesn’t work correctly starts from the step 9. I am not sure how to do it correctly.

One of my attempts to make the output right (the output I get with the code used below is
A
A
E
O
A
E
A
e,e). The letters are not in one line (when I add ‘join()’ there is an error) and the letter E isn’t doubled:

const input = 'a whale of a deal!';

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

const resultArray = [];

for (let l = 0; l < input.length; l++){
 // console.log(l);
  for (let j = 0; j < vowels.length; j++){
   // console.log(j);
 if (input[l]=== vowels[j]){
   console.log(vowels[j].toUpperCase());
 }


}
if (input[l] === 'e'){
  resultArray.push(input[l]);
} 

if (input[l] ==='u'){
  resultArray.push(input[l]);
}
  
};
console.log(resultArray.toString());

If I remove the following lines below from the code :

if (input[l] === 'e'){
  resultArray.push(input[l]);
} 

if (input[l] ==='u'){
  resultArray.push(input[l]);
}

the output will be :

A
A
E
O
A
E
A

Ok lets see if I can help you :wink: .

The doubling not working for e and u, that’s actually very explainable due to a certain action you do before those two if statements, lets see if you can figure that out by yourself. Please come back to me when you figured it out :wink: .

Using join() is the way to go ;). Otherwise you won’t be able to turn the array in a combined string. The join() method however needs an input parameter. Please refer to the documentation on how join works :wink: .

Also I suggest looking into how to proper indent your code, it aids massively in bug fixing. You may have put some of your code outside the scope of where they should be.

Hope this has helped you! Keep on coding!

3 Likes

Thank you.

The code looks now in the following way:

let input = 'a whale of a deal!';

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

let resultArray = [];


for (let l = 0; l < input.length; l++){
 // console.log(l);
  for (let j = 0; j < vowels.length; j++){
   //console.log(j);
if(input[l] === vowels[j]){
  resultArray.push(vowels[j]);
}
  }

 if (input[l] == 'e'){
  resultArray.push(input[l]);
}

if (input[l] ==='u'){
  resultArray.push(input[l]);
}
  
};

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

Here are the changes made: https://prnt.sc/p8zzr5 , https://prnt.sc/p8zztb
The input ‘a whale of a deal!’ now returns the result: AAEEOAEEA that should be correct.

If you would like to add something, you can let me know.

1 Like

Good job, you picked up on the fact that toUpperCase() too early caused the doubling of vowels not working :wink: .

resultArray.join('').toString().toUpperCase()

toString() is not needed since join('') already turns the array into a string :wink: .

And I thought the issue was that I used console.log(vowels[j])
instead of resultArray.push(vowels[j]) but wasn’t sure why.

1 Like

Hey, I misread it then, my bad :stuck_out_tongue: .

But yes when logging the vowels to the console you are not pushing them in an array.

Besides that you were only pushing the e and a to resultArray.

Anyways, Good Job!

1 Like