3

I am attempting to create a basic word game with a branching answer system. I am running into an issue right off the bat concerning prompts. When I write my 'if' statement to bring about another prompt, nothing happens. how can I fix this? The following is a sample of my work:

confirm("You wake up to your mother's voice, 'Wake up! I can't believe you slept in this late! You need to get dressed and hurry on down to Professor Oak's Lab! No time for breakfast! Get going!'");

var questionOne = prompt("1. Hurry out of bed, quickly get dressed, and run out the door! 2. Roll out of bed sleepily, manage to put on your clothes, and make a cup of coffee before leaving the house. 3. Grumble back at your mother and go back to sleep." , "Enter 1, 2, or 3.");

if (questionOne === 1)
{
  prompt("You arrive at Professor Oak's lab in a rush, but haven't quite missed the event! It's time to get your first Pokemon! When you meet the Professor he says with a wink, 'Ah yes, it's you! I remember your mother quite well. Wonder woman... Alright, follow me!' You follow Oak into his lab to find 3 Pokeballs on his desk. 'Choose one,' he says with a hand gesture. [1. Left Pokeball. 2. Middle Pokeball. 3. Right Pokeball.]");
}
Buzinas
  • 11,597
  • 2
  • 36
  • 58
  • Put a breakpoint on the line with `if`. When it stops there, examine the value of the variable `questionOne`. Try typing `questionOne === 1` into the console and note the result. Think carefully about why the condition might not be satisfied; look again at `questionOne` and consider its type. –  Oct 01 '15 at 04:37

2 Answers2

0

Prompt returns a string value. You need to cast your prompt return value as an integer. Or conversely, compare to the string "1".

Option 1:

if (parseInt(questionOne) === 1) {
  prompt("Second tree.")
}

Option 2:

if (questionOne === '1') {
  prompt("Second tree.")
}
Taylor Glaeser
  • 1,338
  • 12
  • 18
0

The prompt function returns a string (since you can add any text you want there).

So, since you're using the === operation - and it doesn't make implicity conversions - when you try to compare the result with a Number, the if-statement will never go through.

You must change your comparison to the == operator, which makes the implicity conversion, or you can change it directly to a string, e.g questionOne === '1'. Take a look at the example below:

confirm("You wake up to your mother's voice, 'Wake up! I can't believe you slept in this late! You need to get dressed and hurry on down to Professor Oak's Lab! No time for breakfast! Get going!'");

var questionOne = prompt("1. Hurry out of bed, quickly get dressed, and run out the door! 2. Roll out of bed sleepily, manage to put on your clothes, and make a cup of coffee before leaving the house. 3. Grumble back at your mother and go back to sleep." , "Enter 1, 2, or 3.");

if (questionOne === '1')
{
  prompt("You arrive at Professor Oak's lab in a rush, but haven't quite missed the event! It's time to get your first Pokemon! When you meet the Professor he says with a wink, 'Ah yes, it's you! I remember your mother quite well. Wonder woman... Alright, follow me!' You follow Oak into his lab to find 3 Pokeballs on his desk. 'Choose one,' he says with a hand gesture. [1. Left Pokeball. 2. Middle Pokeball. 3. Right Pokeball.]");
}
Buzinas
  • 11,597
  • 2
  • 36
  • 58