My WHALE-Excercise code doesn't work properly

Link to the excercise:
https://www.codecademy.com/paths/web-development/tracks/web-dev-js-arrays-loops-objects/modules/learn-javascript-loops/projects/whale-talk

Hello everyone :slight_smile:

I am a beginner at coding and this is my first time really getting stuck. My Whale Talk code is not running the way i want it to. I watched the walktrhough video and i honestly cant find what is different between my code and the code in the video. I thought i checked every single letter, number and sign: The whole syntax. Instead of picking all vowels inside the phrase “valar morghulis” and putting them into an array it puts every single letter 5 times into the array…(Seems like my if condition doesnt work properly / the for loop).

I am sure its some dumb, little mistake that is costing me my sanity :wink:
I hope someone here can help me, i would be really thankfull!! An additional explanation to why my code does not work properly would be even better :slight_smile:

Best regards,

Daniel Kömpf from Germany

Here is my code:

const input ='valar morghulis';
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]===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());

Hi,

Try removing the semicolon; at the end of the if condition

4 Likes

Thats the solution there ;). @louwding

@koedani If I can give you any advice I would suggest you pay a bit more attention on formatting. This aids towards better readability and spotting mistakes in the syntax. Using proper formatting with your code would result in the following:

const input = 'valar morghulis';
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] === 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());
5 Likes

Thanks a lot for your help!!!

I will try to increase my attention for formatting!! I can’t beleive I missed that wrong semicolon :smiley: I actually had the correct formatting for most of the code, but in the process of “trying everything” I changed that^^

2 Likes

Thank you louwding for helping me!

2 Likes

FOR and IF code blocks don’t get semi-colons. The are well delimited already and the parser knows where they start and end.

for (let i = 0; i < input.length; 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]);
      }
    }
  }
}
2 Likes

Hey Roy, thx for the headsup.

I thought it was common practice to use semi-colons behind each closing brackets, oops :stuck_out_tongue: .

1 Like