"Do you want to race Bieber" always spits out the else clause


#1



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


I understand that strings are case-sensitive. I have "yes" in my if-statement, so I input yes when the racing prompt pops up. Still, it never prints "You and Bieber start racing..." It only ever prints "Oh no!..."

I'm not getting any errors. I made it all the way to the end of the lesson (part 7) and it's congratulating me on a job well done. What's going on?


// Check if the user is ready to play!

confirm("Are you ready to play?");
var age = prompt("Please enter your age");
if (age < 13)
{
   console.log("Please consult with a parent or guardian before continuing.");   
}
else
{
   console.log("Welcome! Let's get started!");   
}
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 (prompt==="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.'");
}
var feedback = prompt("Please rate this game on a scale from 0 to 10.");
if (feedback > 8)
{
    console.log("Thank you! We should race at the next concert!");
}
else
{
    console.log("I'll keep practicing coding and racing.");
}


#2

your if statement:

if (prompt==="yes")

you use prompt for comparison, but prompt is the function you use to prompt the user for input, this input is stored is userAnswer, so you should compare userAnswer with yes


#3

D'oh! Thank you! I fixed it and it works now.

Is there a way to accept "yes" and "YES" and possibly even "yes " and all variations in between without hard-coding a whole bunch of nested if clauses? Or is that something taught in later lessons?


#4

it is taught later, but you know what i would do? this:

var userAnswer = prompt("Do you want to race Bieber on stage?").toLowerCase();

see the end, this will convert it to lowercase, it is the easiest solution


#5

That's very elegant! Is there something similar that strips off extraneous spaces? Like if someone were to type "yes " instead of "yes"?


#6

use regex:

var regexp = /yes[ ]+/i;
userAnswer = "yes ";
if (userAnswer.match(regexp)){
  console.log("match");
} else {
  console.log("no match");
}

you can also do: var regexp = /[ ]+yes[ ]+/i;

this will ignore spaces in front of the word. uhm... the [ ] means match single character, in this case a space, the + means multiply of the characters specified in the square brackets. the i is for ignore case.

Regex is useful, but super tricky. This is a relatively simple regex :smiley:


#7

Thank you! I'll add that to my notes. :sunglasses:


#8

i don't think regex is covered in the lesson, and it pretty tricky.


#9

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