5/7 help with code


#1

Hello
this is my code, it works fine but if i choose any answer on the last question its always true, how do i change that??

confirm("Are you ready to play?");
var age = prompt("What's your age");
if (age < 13)
{
console.log("You are to young to play, we are not responsible for any ruined underwear");
}
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= true){

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

First of all, semi colons after your console.logs. Just "=" means "change to" in coding. "===" means "equal to". Try changing the equal sign over there. Also, I don't know if people will actually put "yes" or "no" instead of "true"...

Oh yeah and that's weird.(In a good way) :smiley:


#3

I've imagined that if bieber was in the story it should be a horror movie haha .
One more thing..
They dont ask for this but how do I make the computer knows when the answer is "No" and when the answer is "Yes"?

Thank you so much!


#4

That's an important point, prompt returns always strings so even if you enter true what is stored in userAnswer is rather "true" which can lead to a problem as true and "true" are equal by value, so true == "true" would work but "true" === true would fail (=== checks types as well and here they are boolean vs string). So either use == to comparet them instead of === (= is not an option :smile: ) or use strings all the way like e.g. yes or no in your condition.

@marianogiusto I guess you tell the computer by storing it in userAnswer and using it in an if statement or what do you mean by this?


#5

@haxor789 I mean that if the user answer is "No" i want that to work as a "False"


#6

As said you do this by adjusting your condition e.g.

if(userAnswer == "yes"){
    do stuff
}
else if(userAnswer =="no"){
    do other stuff
}
else{
  console.log("Please answer with 'yes' or 'no'");
}

#7

haha I thought it was going to be more complicated.

Thank you so much for the help =)


#8

Thanks its working,Great help!