-4

Ok, so im a beginner, working with javascript. Im making a small game to impress my friends, and for some reason, this code isnt working. This is only part of my code, the part that has to deal with my it isnt working. When I run it, it doesn't go to the ELSE command, even if you dont type STAY, will someone tell me why? Here it is:

var qestionOne = prompt("An old man emerges from the shadows of the forest that you are currently standing in. Farther ahead, you can see that the forest breaks up, and the trees become less and less thick. If you squint, you can even see a small town. * Do you chose to either, STAY, and talk to the old man, or LEAVE, and continue down the path that you are following, and go to the town? *");

if (questionOne = "STAY") {
    alert("The old man approaches you, and speaks. 'I must warn you, there is a trap at the end of this forest. Were the undergrowth thins,there is a horrable and evil sludge monster. Please, go around' The old man rasped. Thankfull for the information, you proceed your journey, and avoid a big portion of the forest ahead.");
} else {
    alert("You travel ahead, down the path. You suddenlly become very hungry. You notice that the trees around you have very juicy looking fruit on the branches.You have never seen this type of food before, however, you are a..Fruit-toligist...and know that poiseness fruit have yellow and black stripes. You pluck one off of a nearby tree and hungrily munch into the foreign food. A loud screeching sound interupts your thoughts on filling your stomache, and directs your eyes behind you.");

}
rorra
  • 9,593
  • 3
  • 39
  • 61
slowgames
  • 3
  • 1
  • 1
    Typo may be? You've use 'qestionOne' as the variable and in your `if` condition you've used 'questionOne' – Breakpoint Dec 02 '14 at 05:22
  • 4
    `var qestionOne` is spelled differently from `if(questionOne = "STAY")` (the latter is an assignment, which also seems wrong). Your friends will not be impressed. – Thilo Dec 02 '14 at 05:23

5 Answers5

4

The reason is you are using an assigment operator instead of comparing, so you are assigning the value "STAY" to the variable questionOne and the result of such assignment is always true

Instead of

if (questionOne = "STAY") {

you should try

if (questionOne == "STAY") {
rorra
  • 9,593
  • 3
  • 39
  • 61
4
var questionOne = prompt("An old man emerges from the shadows of the forest that you are currently standing in. Farther ahead, you can see that the forest breaks up, and the trees become less and less thick. If you squint, you can even see a small town. * Do you chose to either, STAY, and talk to the old man, or LEAVE, and continue down the path that you are following, and go to the town? *");

if (questionOne == "STAY") {
    alert("The old man approaches you, and speaks. 'I must warn you, there is a trap at the end of this forest. Were the undergrowth thins,there is a horrable and evil sludge monster. Please, go around' The old man rasped. Thankfull for the information, you proceed your journey, and avoid a big portion of the forest ahead.");
} else {
    alert("You travel ahead, down the path. You suddenlly become very hungry. You notice that the trees around you have very juicy looking fruit on the branches.You have never seen this type of food before, however, you are a..Fruit-toligist...and know that poiseness fruit have yellow and black stripes. You pluck one off of a nearby tree and hungrily munch into the foreign food. A loud screeching sound interupts your thoughts on filling your stomache, and directs your eyes behind you.");

}

Two problems: typo in questionOne, and using = intead of ==

philz
  • 1,012
  • 6
  • 11
1

You are using incorrect comparison operator. Use if(questionOne == "STAY") instead of if(questionOne = "STAY")

var questionOne = prompt("An old man emerges from the shadows of the forest that you are currently standing in. Farther ahead, you can see that the forest breaks up, and the trees become less and less thick. If you squint, you can even see a small town. * Do you chose to either, STAY, and talk to the old man, or LEAVE, and continue down the path that you are following, and go to the town? *");

if (questionOne == "STAY") {
    alert("The old man approaches you, and speaks. 'I must warn you, there is a trap at the end of this forest. Were the undergrowth thins,there is a horrable and evil sludge monster. Please, go around' The old man rasped. Thankfull for the information, you proceed your journey, and avoid a big portion of the forest ahead.");
} else {
    alert("You travel ahead, down the path. You suddenlly become very hungry. You notice that the trees around you have very juicy looking fruit on the branches.You have never seen this type of food before, however, you are a..Fruit-toligist...and know that poiseness fruit have yellow and black stripes. You pluck one off of a nearby tree and hungrily munch into the foreign food. A loud screeching sound interupts your thoughts on filling your stomache, and directs your eyes behind you.");
}
Romisha Aggarwal
  • 321
  • 1
  • 13
  • 1
    or `===` (which gets recommended a lot) http://stackoverflow.com/questions/359494/does-it-matter-which-equals-operator-vs-i-use-in-javascript-comparisons?rq=1 – Thilo Dec 02 '14 at 05:26
0

Ok, two answers tell you how to make it work. So I'm gonna tell why else is never reached.

questionOne = "STAY" is an assignment expression. What it does are:

  1. assign string "STAY" to indicator questionOne
  2. return the assigned value ("STAY")

In such case, the if statement is actually if("STAY") {...}. According to JS type coersion "STAY" is converted to boolean true. This is the reason you could never run into else part.

Leo
  • 13,428
  • 5
  • 43
  • 61
-1

Focus on letter case and spelling as this gets most 'early' programmers.

var qestionOne

is not

questionOne
Syeda Zunaira
  • 5,191
  • 3
  • 38
  • 70