JS Lesson 5, The story heats up problem


#1

I am not sure where I am going wrong. I am getting stuck on the last if/else statement that checks condition of "userAnswer". the error i see is SyntaxError: missing ) after argument list. Not sure but I think this is due to if else is checking for string yes but that is not defined in my variable userAnswer. What am I missing? It's not logical to me that the variable userAnswer is nothing but a string of a sentence. Should that variable userAnswer be yes/no or true/false?

// Check if the user is ready to play!
confirm("Are you ready to play?")

// Declared variables
var age = prompt("What's your age");
var userAnswer = prompt("Do you want to race Bieber on stage?");

// evaluate age with if else statements
if(age < 13)
{
console.log("your age is allowed to play but we take no responsibility.");
}
else (age > 13)
{
console.log("Welcome, please enjoy the game!");
}

// print out an intoduction
console.log("You are at a Justin Bieber concert, and you hear this lyric 'Lace my shoes off, start racing.'");

// the begining dialog of the game
console.log("Suddenly, Bieber stops and says, 'Who wants to race me?'");

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


#2

in else statement you don't have to specify any condition.


#3

Hi Sureshdev, including anything with else made no difference I tested it with === and !== "no" earlier. Note it would make no sense to use "Answer !== "no"); that would be like saying if yes print out the last string. No would print nothing when something needs to print. My point is how does prompt a string pair up with yes or no for the if else portion of userAnswer. I would understand if the variable was var userAnswer = prompt("yes"); but having userAnswer = prompt ("Do you want to race Bieber on stage?"); just don't make sense.

Would it make sense to include confirm( yes / no ); to get user input? This brings me to another question, earlier there is a prompt for age and nothing ever pops up even with my popup blocker off. I think that there is something really wrong with the code provided to us.


#4

else cannot take a condition, it isn't allowed. Prompt allows someone to input yes or no. Refresh your browser to fix prompt issue.


#5

Hi cadecodes, that didn't solve my problem. I even tried to reboot my mac. The prompts are not popping anything up for me to input data. cadecodes this is my code once I followed your suggestions.

// Check if the user is ready to play!
confirm("Are you ready to play?")

// Declared variables
var age = prompt("What's your age");
var userAnswer = prompt("Do you want to race Bieber on stage?");

// evaluate age with if else statements
if(age < 13)
{
console.log("your age is allowed to play but we take no responsibility.");
}
else
{
console.log("Welcome, please enjoy the game!");
}

// print out an intoduction
console.log("You are at a Justin Bieber concert, and you hear this lyric 'Lace my shoes off, start racing.'");

// the begining dialog of the game
console.log("Suddenly, Bieber stops and says, 'Who wants to race me?'");

// if else evalution "userAnswer" variable
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.'"):
}


#6

Ok, problem solved after i tested this with JSLINT.com. (Note)It is also important that the prompt is located with the if else code or it just don't work the same.
The following code made everything run smoothly and without any errors. it was on my last console.log line it had : and not; lol!!!

// Check if the user is ready to play!
confirm("Are you ready to play?")

// Declared variables
var age = prompt("What's your age");
//var userAnswer = prompt("Do you want to race Bieber on stage?", "yes");

// evaluate age with if else statements
if(age < 13)
{
console.log("your age is allowed to play but we take no responsibility.");
}
else
{
console.log("Welcome, please enjoy the game!");
}

// print out an intoduction
console.log("You are at a Justin Bieber concert, and you hear this lyric 'Lace my shoes off, start racing.'");

// the begining dialog of the game
console.log("Suddenly, Bieber stops and says, 'Who wants to race me?'");

var userAnswer = prompt("Do you want to race Bieber on stage?", 'yes');
// if else evalution "userAnswer" variable
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.'");
}