Else if Statements

Can someone explain why the position of the last curly bracket breaks this lesson?

let moonPhase = "mostly full";

if(moonPhase === "full")
  {console.log("Howl!");}

else if(moonPhase === "mostly full")
  {console.log("Arms and legs are getting hairier");}

else if(moonPhase === "mostly new")
  {console.log("Back on two feet");}

else 
  {console.log("Invalid moon phase");}

i get this:
Did you write an else statement that logs ‘Invalid moon phase’?

but if i change the else statement to

  {console.log("Invalid moon phase");
}

Its seems to me either way is valid javascript syntax because it logs the else statement either way i have it but it fails the lesson due to the position of the curly bracket. Is it bad convention to keep it on one line?

Although you are free to choice, by far the two most common styles are:

else {
   /* code */
}

and:

else 
{
   /* code */
}

and by most common i mean 99%, if not more. Although this isn’t an official rule, decades of programming has learned that this are the two best styles.

2 Likes

its gonna take some time and practice getting used to. I started out learning python. I want to make sure i learn the best typing conventions which Codecademy doesn’t seem to explain to well the importance of it. I know its a lot better to stick to what everyone is used too.
Thanks for the quick reply so i should probably have it like this i guess.

else
{
console.log("Invalid moon phase");
}
1 Like

This looks like the most presentable way to do it

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.