一个给朋友留下深刻印象的小代码就是工作

时间:2014-12-02 05:19:42

标签: javascript if-statement

好的,所以我是初学者,使用javascript。我正在制作一个小游戏来打动我的朋友,由于某种原因,这段代码不起作用。这只是我的代码的一部分,必须处理我的部分不起作用。当我运行它时,它不会进入ELSE命令,即使你没有键入STAY,有人会告诉我为什么吗? 这是:

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

}

5 个答案:

答案 0 :(得分:4)

原因是您正在使用分配运算符而不是比较,因此您将值“STAY”分配给变量questionOne,并且此类赋值的结果始终为true

而不是

if (questionOne = "STAY") {

你应该试试

if (questionOne == "STAY") {

答案 1 :(得分: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.");

}

两个问题:问题一是拼写错误,而使用=而不是==

答案 2 :(得分:1)

您使用的是不正确的比较运算符。   使用if(questionOne ==" STAY")而不是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.");
}

答案 3 :(得分:0)

好的,两个答案告诉你如何让它发挥作用。所以我要告诉为什么else永远不会到达。

questionOne = "STAY"是一个赋值表达式。它的作用是:

  1. 将字符串"STAY"分配给指标questionOne
  2. 返回指定值("STAY"
  3. 在这种情况下,if语句实际上是if("STAY") {...}。根据JS类型,coersion "STAY"被转换为布尔true。这就是你永远不会遇到else部分的原因。

答案 4 :(得分:-1)

专注于信件案例拼写,因为这会让大多数“早期”程序员感到高兴。

var qestionOne

不是

questionOne