Reputation: 1
I am learning to code javascript, and have written this code
var userAnswer = prompt("are you feeling lucky, punk?");
if (userAnswer === yes)
{
console.log("Batman hits you very hard. It's Batman and you're you! Of course Batman wins!");
}
else
{
console.log("You did not say yes to feeling lucky. Good choice! You are a winner in the game of not getting beaten up by Batman.");
}
But when I run it, I get ReferenceError: yes is not defined.
Upvotes: 0
Views: 1854
Reputation: 4470
You are trying to compare the variable userAnswer
with another variable yes
. The problem is that you have not defined yes
. I believe you are trying to compare userAnswer
with the string literal "yes".
So if you changed userAnswer === yes
to userAnswer === "yes"
it should do what you want.
Upvotes: 0
Reputation: 39532
When you don't enclose yes
in quotes (i.e. a string literal), JavaScript looks for a variable with the name yes
. You haven't defined a variable with the name yes
, so it throws your error.
Add quotes as such to determine if userAnswer
contains the string yes
:
if (userAnswer === 'yes')
If you want to accept different cases (i.e. Yes
and YES
), you can use .toLowerCase()
if (userAnswer.toLowerCase() === 'yes')
Upvotes: 1