Why does is come up with a syntax error for 'else'? Part 5/7


#1

confirm("I am ready to play!");
var age = prompt("What is your age")
if (age <= 13)
{
console.log("You are allowed to play but we take no responsibility");
}
else;
{
console.log("LETS PLAY!");
}
console.log("You are at a Justin Bieber concert, and you hear this lyric 'Lace my shoes off, start racing.'");
console.log("Suddenly, Bieber stops and says, 'Who wants to race me?'");
var userAnswer = prompt("Do you want to race Bieber on stage?");
if (userAnswer === "yes");
{
console.log("You and Bieber start racing. It's neck and neck! You win by a shoelace!");

}
else;
{
console.log("Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'");
}

// Keeps saying there is something wrong with my final else statement. Please help. Thank you


#2

Hello.

You should not put semicolons after if and else statements. You can find a great article about semicolons usage here -> Your Guide to Semicolons in JavaScript.

The problem is that when we have code like this:

if(3 > 12); {
    console.log("3 is greater than 12!");
}

it will still print this text, because semicolon terminates our if statement.


#3

Thank you so much :smiley:


#4

You're very welcome :smile:


#5

Hello!
I try to put the code like hint by factoradic, but it doesn't work i'm becoming crazy... can someone help me please??

if (userAnswer = yes);
{
console.log("You and Bieber start racing. It's neck and neck! You win by a shoelace!");
}
else
{
console.log("Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'");
}

i don't understand I have every day same error syntax for 'else'

Thank to everyone ....


#6

As I said, you should not put a semicolon after if statement.

It terminates your if statement and then your else returns error, because else must be part of if ... else construction.

There is also one more problem in your code. In the conditional part of your if statement you want to put expression that evaluates to true or false. Usually we use comparison operators, like >, !== etc.

This statement:

userAnswer = yes

is an assignation operation. It says: set variable userAnswer to value of variable yes.

  1. You don't want to assign to this variable new value, you want to check if this variable is equal to some value => you have to use equality check operator ===.
  2. Variable yes is undefined. You want to use a string => "yes".

Corrected code:

if (userAnswer === "yes") {
    console.log("You and Bieber start racing. It's neck and neck! You win by a shoelace!");
} else {
    console.log("Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'");
}

#7

The Javascript-Interpreter
interprete's a semi-colon-; as an End-of-Statement indicator.

As you used a semi-colon-; directly AFTER your ELSE
the ELSE-code-block-{ code-block } will never be reached....


#8

Thank you so much... you have reason!!!!


#9

Hello!
this is my code:

// Check if the user is ready to play!
console.log("You are at a Justin Bieber concert, and you hear this lyric 'Lace my shoes off, start racing.'");
console.log("Suddenly, Bieber stops and says, 'Who wants to race me?'");
var userAnswer = prompt("Do you want to race Bieber on stage?");

if (userAnswer === "yes") {
console.log("You and Bieber start racing. It's neck and neck! You win by a shoelace!");
} else {
console.log("Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.");
}

I can't find any error but still I can't save & submit (Error: Oops, try again. Did you add an if statement to your code?).
Anybody can help me? Thanks


#10

Your code is correct.

This course is buggy and SCT (submission correctness test) expects that you have a code from the previous exercise at the top of your script. Just add this code at the beginning of your program:

confirm("Ready to play?");
var age = prompt("What's your age");
if (age < 13) {
    console.log("You're allowed to play but you take no responsibility.");
} else {
    console.log("Have fun");
}

#11

You're very welcome :smile:


#12

Yes it works!
Thank you very much factoradic


#13

Great! :smile:

You're welcome.


#14

I need help on mine, here's my code:

// Check if the user is ready to play!
confirm("Make sure that you are old enough to play this game");
var age = prompt("What's your age?");

if (age < 13) {
console.log("You're allowed to play, but you take no responsibility.")
} else {
console.log("Play on!")
}
//Introduction
console.log("You are at a Justin Bieber concert, and you hear this lyric 'Lace my shoes off, start racing.'")

//Storyline

console.log("Suddenly, Bieber stops and says, 'Who wants to race me?'");

var varuserAnswer = prompt("Do you want to race Bieber on the stage?");

//If Else statements base on choices the player makes

if (userAnswer === "Yes") {
console.log("You and Bieber start racing. It's neck and neck! You win by a shoelace!");
} else{
console.log("Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'");

Can anyone tell me the problem? It keeps on saying unexpected end of input


#15

I fixed the problem, but it keeps on saying that userAnswer isnt defined.....even though I do it right like this: var varuserAnswer = prompt(blah blah blah)


#16

nvm....I figured it out.......