2

I am learning Javascript via Codecademy and no have been stumped on this little piece here.

I have supposed to write an if else statement.

It shows me here in the following that there is a Syntac Error with a missing identifier:

var userAnswer = prompt("Are you feeling lucky, punk?");

if (userAnswer === "yes");
{

    console.log("Batman hits you very hard. It's Batman and you're you! Of course Batman wins!");
}

 else {

    console.log("You did not say yes to feeling lucky. Good choice! You are a winner in the game of not getting beaten up by Batman.");
}

What is wrong with that.... There is no error in this example here:

if (age < 18)

{

    console.log("We take no actions or responsibility. Play at your own risk!");
}

else

{

    console.log("Enjoy the game");
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
user3260811
  • 23
  • 1
  • 1
  • 3
  • 2
    It would be easier to debug, if you'd use [recommended code conventions](http://javascript.crockford.com/code.html) for JS. – Teemu Feb 01 '14 at 16:25

4 Answers4

4
if (userAnswer === "yes");

Remove the semicolon.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • 1
    And why so? Why can I not have one? Is it like ending the statement? – user3260811 Feb 01 '14 at 16:24
  • with `;` you are telling the script to stop rendering the rest of the statement conditions. It's because `if()`, it must have condition which therefore starts at `{ }`. If `if()` doesn't see the condition statement, it stops rendering. – Faron Feb 01 '14 at 16:25
3

There's a semi-colon after the first conditional check. Also, you should always put the opening bracket of the conditional branch on the same line as the brackets

danwellman
  • 9,068
  • 8
  • 60
  • 88
0
var age;
age = prompt('How old are you?');
if (age < 18)

{

alert("We take no actions or responsibility. Play at your own risk!");
}

else if(age > 18)

{

alert("Enjoy the game");
}
George
  • 11
0

remove the semicolon after

if (userAnswer === "yes");

if you put the semicolon there, you are telling the script to stop there and not to render the next conditional statement that is "else"[SyntaxError: Unexpected token else]

1

vimuth
  • 5,064
  • 33
  • 79
  • 116