Help with if/else statments


#1


https://www.codecademy.com/en/courses/learn-javascript/lessons/control-flow/exercises/comparison-operators-ii


The prgram has some issue with the else statment but I am doing what it said...


var moonPhase = 'full';

if (moonPhase === 'full');
{
  console.log("howl");
} else {
  
  console.log('i swear i am not a werewolf');
}


#2

Here's the if / else statement structure

if (condition) {
  // do something
} else {
  // do something else
}

Something in your code's not quite right


#3

var moonPhase = 'full';

if (moonPhase === 'full'); {
console.log('howl');
} else {
console.log('i am not a werewolf');
}

I tried this and still getting an error for the 'else' statement. It says syntax error , unexpected token "else"


#4

Check my example once more, but don't focus on the else line, that's not where the problem is...


#5

So you can have a ';' on the //do something lines but not before the first curly bracket??

I removed the first semi colon and it worked. Thanks for the hasty response.


#6

That's right.

By putting the semicolon after the if line, you told the program to move on to something else straight away, which is why you got the else error. The program basically found a random else hanging around without an if. It didn't process the rest of the if/else statement because of that semicolon.


#7

Got it- thanks again!


#8

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