If else on "yes" but what if user says "YES"


#1


javascript, code your own adventure, exercise 5

does not behave incorrectly, just curious about something

What happens if the user is prompted and writes YES or YeS instead of all lowercase yes?
Is there a way to code for this?


// Check if the user is ready to play!
confirm("I am ready to play the game!");
var age = prompt("What's your age?");
if (age < 13) {
    confirm("You are allowed to play but we take no responsibility."); 
    console.log('age not acceptable');
} 
else 
{console.log("age acceptable"); 
confirm("Go get em! ");
}
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

Yes, there is, actually! By doing var answer = prompt("Enter answer").toLowerCase();. This will take the users input, and transfer it to lower case. You can also do .toUpperCase();. So this way, no matter what the user says, as long as its "yes," it'll be correct.


#3

thank you!
as I understand this, you only need to do one or the other (lower or upper case). you don't need to code it for both ways, right?


#4

Yup! Only one is required, but make sure if you do an if statement you make compare the var as it upper case or lower case form! I'll show an example:

var answer = prompt("Enter answer").toUpperCase();

if (answer === "YES") { /* make sure the YES is capitalized or lower case, depending on which you set it as */
  do this
}

Make sense?


#5

You people make me feel dumb... I thought I was smart. I'm jealous


#6

Haha, it's all knowledge from Codecademy and YouTube, my friend :slightly_smiling:


#7

Hey can you message me please?


#8

Thank you quobogeba!


#9

Hey Quobogeba,

I used it in the conclusion (exercise 13) to 'introduction to for loops in js'

Check out my code.

var programmingConcepts = ["variables", "loops", "if/else clauses", "functions"];

var answertoQuestion = prompt("Do you like programming").toLowerCase();

if (answertoQuestion === "yes") {
for (var i = 0; i < programmingConcepts.length; i++)
console.log("I am learning " + programmingConcepts[i])
}
else {
confirm("Maybe programming is not for you?");
}

// it works!!!


#10

@evanqs I'm glad it works! Have fun coding! :slightly_smiling:


#11

My program is **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.'");
}
**

but when I save and submit it shows an error saying "Oops! Try Again. Did you add an if to your code?"

What do i do?


#12

Sorry, I can't see the mistake, but I can give you the codes I've used:

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.'")
}