The Story Heats Up! Syntax Error


#1



https://www.codecademy.com/courses/javascript-beginner-en-x9DnD/0/5


There is a problem with my synatx, but I do not see it, maybe a fresh set of eyes would help?


// Check if the user is ready to play!
confirm ("I am ready to play")
var age = prompt("What is your age");
if(age < 13) {
    //console.log("You can play, but we take no responsibility for any inappropriate material")
} else {
    //console.log("You can 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'")
}


#2

Why are the console.log statements all comments //console.log(..)?


#3

And here yes doesn't need to have an opening parentheses. Just leave it like,

if (userAnswer === "yes")


#4

I solved it with my first try and without looking in the forum

~~~~~
console.log("You are at a Justin Bieber concert, and you hear this lyric 'Lace my shoes off, start racing.")

var age = prompt("What's your age");

if(age < 13)
{
console.log("You are younger than 13")
}
else // "otherwise"
{
// do this code
console.log("You are older than 13")
}
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 (condition = "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.'")
}

~~~~~


#5

Really good! :grinning:


#6

Yeah, but you have a couple of mistakes the most severe one being the condition of the if statement. What you wrote is not a comparison but an assignment operation.


#7

Sorry, But I didnt know of that. i went strictly by the instructions and it made me pass.
Can you clarify what you meant by "assignment operation" and comparison please?


#8

Actually the instructions warn you about this as well.

The difference is that when you use a single equal sign you are assigning a value to a variable. When you use 2 or 3 (== or ===) you are comparing 2 values.

Examples:

v1 = 2; // From now on, until changed, v1 will return 2
v1 === 3 // will return boolean value false since v1 is 2 rather than 3

In the context of your if statement

if (condition = "yes") {
// blah blah
} else {
// yada yada
}

you will always get "blah blah" since you assign "yes" as the value of condition at the time of checking. The operation will always fall in the true block because condition equals to "yes", hence it's true by nature (unlike 0, '', null, false).

I hope this makes sense.

EDIT: The correct syntax is

if (condition === "yes") {
// blah blah
}


#10

hey but whats "condition" here and you are assigning string "yes" to the condition(unknown word here), this is always true. ( correct me if i am wrong ).


#11

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