"userAnswer" returns same log no matter the input. Why?


#1



Code Your Own Adventure!: The story heats up!


My code in this excercise isn't really behaving "incorrectly" but it is not logical. Whatever the user inputs for the first prompt will always return the console.log in the if statement as long as the user clicks "OK" on the prompt window. How can I fix this logic so that it searches for specific responses no matter if the user clicks on "OK" or not?

Here is my code:

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

***I set the "||" within the second "if" condition to create more specific positive inputs. ***

How can i fix this so that if someone inputs "No" or any other answer other than the three listed conditions and presses "OK" that the else block is executed?

Thanks a bunch in advance for reading! :v:


#2

@mcordero1206

You are supposed to be putting conditions. When you compare if userAnswer === "yes", it's a condition but what about the others? "Yes" or "YES" are not conditions. || are used for separating conditions so you still have to do userAnswer === "yes" || userAnswer === "Yes" || userAnswer === "YES"..


#3

@miniapple8888

Thank you for your quick reply! I see what you mean about the condition separator. Thanks for clearing that up! But that still doesn't help me solve the issue of the ambiguous user input. I've experimented with if else statements but have been unsuccessful. I want to write code that will return the else statement if the input is "no" or anything other than "yes" or any variation of the word. Any thoughts? :thought_balloon::thought_balloon:


#4

@mcordero1206

Can you post your code back so I can have a look to what you've updated in your code?


#5

Sure! Here is my full code so far so you can test it yourself*

confirm("Do you want to play a game?");
var age = prompt("What is your age?");

if(age < 13)
{
    console.log("Under 13? Play at your own risk!");
}
else
{ 
    console.log("Lets 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" ||userAnswer ===  "Yes" ||userAnswer ===  "YES")
{
    console.log("You and Bieber start racing. It's neck and neck! You win by a shoelace!");
}
else if(userAnswer === "no")
{
    console.log("Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'");
}
else
{
  //Still trying to figure out what to excute as a default for this block.   
}

*Would you know if there is there a way to test this JS outside of the codecademy IDE?

:v:


#6

@mcordero1206

In your else statement (because you already test whether userAnswer is yes or no), you can console.log some text if the userAnswer is other than yes or no as answer.

*Yes you can. In this case, you can download a text editor to work with JS or use the console in your F12 bar to debug.


#7

Here is my updated code:

confirm("Do you want to play a game?");
var age = prompt("What is your age?");

if(age < 13)
{
    console.log("Under 13? Play at your own risk!");
}
else
{ 
    console.log("Lets 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 if(userAnswer === "no")
{
    console.log("Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'");
}
else
{
  colsole.log("Please input 'yes' or 'no);  
}

But now i'm getting a SyntaxError: Unexpected token ILLEGAL

:confounded:


#8

@mcordero1206

Look here

colsole.log???? Sorry haven't heard of that function before. :wink:


#9

Haha typo, my bad! I fixed it but it still return the same error!


#10

@mcordero1206

Don't forget to close your quotes here.


#11

AHH! Such a simple oversight! I think that's my cue to go to sleep now. :sleeping:

It works now! :bulb:

Haha thanks so much for all your help!

:v:


#12

I had a thought, but it didn't work either.


#13