Error with if/else statement


#1

https://www.codecademy.com/courses/learn-javascript/lessons/control-flow/exercises/comparison-operators-ii?action=lesson_resume&link_content_target=interstitial_lesson


I've tried to do this exercise using an if/else statement, but it doesn't like my else after the first block. When I remove the else and semicolon it will run (incorrectly), and I will be able to move on. Is there something wrong with my code or is it a problem on codecademy's end?


This is the error I'm getting from the compiler:

/home/ccuser/workspace/javascript_101_Unit_2_v3/werewolf.js:4
} else; {
^^^^
SyntaxError: Unexpected token else
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)


This was my code:

var moonPhase = 'full';
if(moonPhase === 'full'); {
  console.log('Howwwwlll!!');
} else; {
  console.log('I swear I am not a werewolf...');
}

#2

Hello,

Just remove both of the semicolons below and it should work.


#3

Thanks! Is there a reason why I would want to remove these semicolons? I thought they were used to end lines of code


#4

The semicolons indicate end of statements, and are used to separate one statement from another. The if/else syntax, along with for, while etc... does not contain semicolons. Here is a useful guide on semicolons for JS, take a look. I'm not really good at explaining things :frowning:

Semicolons in JavaScript

Hope it helps!


#5

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