8. My code keeps coming back with SyntaxError Expected ';'


#1

This is my code.

 var awesome = "Coding is awesome";
 
 while (awesome.length < 17) {
 console.log (awesome);
 awesome + 3;
 }
 
 for (var cat = 2, cat < 4, cat++){
 console.log ("Cats rule the world!");
 }

Whenever I try to run the code I keep getting "SyntaxError Expected ';' ".
Could someone tell me what I'm doing wrong and/or tell me where the ";" goes? Thanks.


#2

for (var cat = 2, cat < 4, cat++){

change the commas to semi-colons " , " to " ; "

for (var cat = 2 ; cat < 4 ; cat++){

#3

Thanks it works now :grinning:


#4

Can someone explain why there is an "awesome = 3"?


#5

Do you mean "awesome + 3"?


#6

Probably yes. Could you explain what you tried to do their? Because atm this line doesn't help and isn't even executed as "Coding is awesome" already has 17 letters or did I counted wrong?


#7

The code

  while (awesome.length < 17) {
  console.log (awesome);
  awesome + 3;
  }

will never run unless you change either "17" to a greater number, or change the length of awesome.
To be honest I'm not sure why I made it so the code will never run, but I can always go back and change it.


#8

Hi. Forgive me but I don't understand the while loop. The condition awesome.length<17 means that if you change it to say 18, this will be an infinite loop. Also the 'awesome + 3' doesn't make sense to me. Can you add a numeral to a variable that contains a string?


#9

The while loop is pretty simple as long ( or better while) the condition is true it will repeat what is written in the {}. So it goes:

check condition -> run {} -> check condition -> {} -> ...

unless the condition is false, then it stops and you go on after the }. So as the string currently stored in awesome has a length of 17 the loop condition is false and therefore you skip it. If it would be 18 the condition is true and unfortunately it will stay true all the time as there is no statement to change awesome so because of this staying true you have an infinite loop. awesome + 3; looks like it is changing awesome but unfortunately it just computes a value that is never again used. Last but not least you could add a number to a string which in this case would give "Coding is awesome3" not necessarily making sense but ok