5. The Story Heats Up


#1

Does anyone know why this part doesn't work? if (userAnswer === "yes" || "Yes")
When I do this, the no answer (the else part) doesn't come out.

The || is an 'or' statement. Is it because we haven't learned it yet?

Here is the entire code so far:
confirm("I am ready to play!");

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

if(age < 13){
console.log("Allowed, but I take no responsibility");
}
else {
console.log("Play On!");
}

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" || "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

Hi @goldiefish

If it may help please see the code example below with comment. Just a tiny error here. When using the AND '&&' or the OR '||' operators, remember to compare the entire expression that should be evaluated, to avoid errors.
In this case what was occurring was you set the if statement to evaluate against the var 'userAnswer' in the first part, but noting in the second and this was causing your output to be incorrect. by setting the expression explicitly you avoid such problems =)

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

if(age < 13){
console.log("Allowed, but I take no responsibility");
}
else {
console.log("Play On!");
}

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" || userAnswer === "Yes") { //when using the OR '||' operator you want to compare the entire expression for each expression that should be evaluated
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.'");
}

#3

Okay, how do I compare the entire expression?


#4

It's already in the answer =). Please look at the code example with the commented line.

if (userAnswer === "yes" || userAnswer === "Yes") { //when using the OR '||' operator you want to compare the entire expression for each expression that should be evaluated

#5

Oh, thanks so much! I didn't see that the first time!


#6

I am getting the following error message "Did you add an if statement to your code?" even though the entire code runs perfectly.I am listing it below..please help, cant understand where am I going wrong

confirm("are you 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.'");
}


#7

Hi @saurabh071185

Could you paste all your code if you are still having problems? If however this is your entire code then may I suggest that you go over exercise #2 'Old enough to play?', which also have a requirement for an if statement, it is possible, and taking a guess without seeing the whole code, this may be the if statement you are missing to pass the lesson? =)


#10