【发布时间】:2016-01-17 15:37:19
【问题描述】:
这个程序会给出三个路线选择,然后询问装备、鞋子和宠物。全部选择后,我使用switch() 语句根据用户的选择给出各自的答案。
问题是响应始终是我对if() 条件的响应,这意味着如果条件不满足,它仍然会记录相同的响应。
这是代码学院的一个练习;该网站说我已经满足了该程序的要求,但是当然,由于结果不正确,我想我会寻求帮助。感谢所有提前帮助的人。 :)
var user = prompt("Your path diverges to three roads, which one will you take?", "Forest, Mountain or Sea?").toUpperCase();
var gear = ["Oxygen tank", "Fire Starter", "Camp"]
var askGear = prompt("Choose your gear:", gear[0] + ", " + gear[1] + " or " + gear[2]).toUpperCase();
var shoes = ["Swimfin", "Studded", "BearPaw"]
var askShoes = prompt("Choose your shoes:", shoes[0] + ", " + shoes[1] + " or " + shoes[2]).toUpperCase();
var pet = ["Monkey", "Wolf", "Whale"]
var askPet = prompt("Choose your pet:", pet[0] + ", " + pet[1] + " or " + pet[2]).toUpperCase();
var choiceBank = [askGear, askShoes, askPet]
var choices = confirm("You chose to take " + askShoes + " shoes, " + askGear + " and a " + askPet + " to your adventure in the " + user + ". Are you ready to go?")
var choiceConfirm = confirm("This is your final chance, you sure?")
switch(user) {
case "FOREST": {
if (choiceBank[0] = 'CAMP' || 'FIRE STARTER', choiceBank[1] = 'BEARPAW', choiceBank[2] = 'WOLF' || 'MONKEY') {
console.log("Congratulations! With those right supplies you chose, you survived to live another day!")
} else {
console.log("Really? You though you could survive with those supplies?")
}
}
break;
case "MOUNTAIN": {
if (choiceBank[0] = 'CAMP' || 'FIRE STARTER', choiceBank[1] = 'STUDDED', choiceBank[2] = 'WOLF') {
console.log("Wow! You're a survival expert!")
} else {
console.log("Really? You though you could survive with those supplies?")
}
}
break;
case "SEA": {
if (choiceBank[0] = 'OXYGEN TANK', choiceBank[1] = 'SWIMFIN', choiceBank[2] = 'WHALE') {
console.log("Congratulations on choosing well, you survived to live another day!")
} else {
console.log("Really? You though you could survive with those supplies?")
}
}
break;
default: {
console.log("Sorry, one of the responses was invalid, please try again.")
}
}
【问题讨论】:
-
您的
if (choiceBank[0] = 'CAMP' || 'FIRE STARTER', choiceBank[1] = 'BEARPAW', choiceBank[2] = 'WOLF' || 'MONKEY') {很有道理。逗号,应该做什么?作业应该怎么做? or||应该怎么做? -
逗号应该添加另一个条件,如果选择了任何一个元素,或者应该使条件为真。
-
但这不是javascript。 and 是
&&并且优先级在 or||之前,所以任何 or 必须在括号中。 -
那么您认为我可以如何改进和纠正它?
-
除了 Nina 指出的问题之外,您还在 if 语句中分配变量而不是比较它们。
标签: javascript arrays switch-statement