Codecademy JavaScript 中的控制流编程
Control flow programming in JavaScript on Codecademy
这个程序会给出三个路线选择,然后询问装备、鞋子和宠物。全部选好后,我用一个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.")
}
}
发生了什么变化:
var
块,现在压缩为 ,
分隔
- statements被
;
隔开
case
块不需要多余的{}
if
改了一些更有意义。
原代码
if (choiceBank[0] = 'CAMP' || 'FIRE STARTER',
choiceBank[1] = 'BEARPAW',
choiceBank[2] = 'WOLF' || 'MONKEY') {
// ^ ^ ^
// assignment or and comma
改为
if ((choiceBank[0] === 'CAMP' || choiceBank[0] === 'FIRE STARTER') &&
choiceBank[1] === 'BEARPAW' &&
(choiceBank[2] === 'WOLF' || choiceBank[2] === 'MONKEY')) {
choiceBank[0] = 'CAMP'
是一个赋值,但是我们需要和choiceBank[0] === 'CAMP'
比较,和'FIRE STARTER'
一样。这应该是 'CAMP'
选择的替代方法。这是通过 logical or ||
实现的。 ,
被 和 &&
取代。 or的优先级比and小,需要加括号。
- 小改动:添加输出而不是
console.log
var user = prompt("Your path diverges to three roads, which one will you take?", "Forest, Mountain or Sea?").toUpperCase(),
gear = ["Oxygen tank", "Fire Starter", "Camp"],
askGear = prompt("Choose your gear:", gear[0] + ", " + gear[1] + " or " + gear[2]).toUpperCase(),
shoes = ["Swimfin", "Studded", "BearPaw"],
askShoes = prompt("Choose your shoes:", shoes[0] + ", " + shoes[1] + " or " + shoes[2]).toUpperCase(),
pet = ["Monkey", "Wolf", "Whale"],
askPet = prompt("Choose your pet:", pet[0] + ", " + pet[1] + " or " + pet[2]).toUpperCase(),
choiceBank = [askGear, askShoes, askPet],
choices = confirm("You chose to take " + askShoes + " shoes, " + askGear + " and a " + askPet + " to your adventure in the " + user + ". Are you ready to go?"),
choiceConfirm = confirm("This is your final chance, you sure?");
switch (user) {
case "FOREST":
if ((choiceBank[0] === 'CAMP' || choiceBank[0] === 'FIRE STARTER') && choiceBank[1] === 'BEARPAW' && (choiceBank[2] === 'WOLF' || choiceBank[2] === 'MONKEY')) {
out("Congratulations! With those right supplies you chose, you survived to live another day!");
} else {
out("Really? You though you could survive with those supplies?");
}
break;
case "MOUNTAIN":
if ((choiceBank[0] === 'CAMP' || choiceBank[0] === 'FIRE STARTER') && choiceBank[1] === 'STUDDED' && choiceBank[2] === 'WOLF') {
out("Wow! You're a survival expert!");
} else {
out("Really? You though you could survive with those supplies?");
}
break;
case "SEA":
if (choiceBank[0] === 'OXYGEN TANK' && choiceBank[1] === 'SWIMFIN' && choiceBank[2] === 'WHALE') {
out("Congratulations on choosing well, you survived to live another day!");
} else {
out("Really? You though you could survive with those supplies?");
}
break;
default:
out("Sorry, one of the responses was invalid, please try again.");
}
function out(s) {
var node = document.createElement('div');
node.innerHTML = s + '<br>';
document.getElementById('out').appendChild(node);
}
<div id="out"></div>
这个程序会给出三个路线选择,然后询问装备、鞋子和宠物。全部选好后,我用一个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.")
}
}
发生了什么变化:
var
块,现在压缩为,
分隔- statements被
;
隔开
case
块不需要多余的{}
if
改了一些更有意义。
原代码
if (choiceBank[0] = 'CAMP' || 'FIRE STARTER',
choiceBank[1] = 'BEARPAW',
choiceBank[2] = 'WOLF' || 'MONKEY') {
// ^ ^ ^
// assignment or and comma
改为
if ((choiceBank[0] === 'CAMP' || choiceBank[0] === 'FIRE STARTER') &&
choiceBank[1] === 'BEARPAW' &&
(choiceBank[2] === 'WOLF' || choiceBank[2] === 'MONKEY')) {
choiceBank[0] = 'CAMP'
是一个赋值,但是我们需要和choiceBank[0] === 'CAMP'
比较,和'FIRE STARTER'
一样。这应该是 'CAMP'
选择的替代方法。这是通过 logical or ||
实现的。 ,
被 和 &&
取代。 or的优先级比and小,需要加括号。
- 小改动:添加输出而不是
console.log
var user = prompt("Your path diverges to three roads, which one will you take?", "Forest, Mountain or Sea?").toUpperCase(),
gear = ["Oxygen tank", "Fire Starter", "Camp"],
askGear = prompt("Choose your gear:", gear[0] + ", " + gear[1] + " or " + gear[2]).toUpperCase(),
shoes = ["Swimfin", "Studded", "BearPaw"],
askShoes = prompt("Choose your shoes:", shoes[0] + ", " + shoes[1] + " or " + shoes[2]).toUpperCase(),
pet = ["Monkey", "Wolf", "Whale"],
askPet = prompt("Choose your pet:", pet[0] + ", " + pet[1] + " or " + pet[2]).toUpperCase(),
choiceBank = [askGear, askShoes, askPet],
choices = confirm("You chose to take " + askShoes + " shoes, " + askGear + " and a " + askPet + " to your adventure in the " + user + ". Are you ready to go?"),
choiceConfirm = confirm("This is your final chance, you sure?");
switch (user) {
case "FOREST":
if ((choiceBank[0] === 'CAMP' || choiceBank[0] === 'FIRE STARTER') && choiceBank[1] === 'BEARPAW' && (choiceBank[2] === 'WOLF' || choiceBank[2] === 'MONKEY')) {
out("Congratulations! With those right supplies you chose, you survived to live another day!");
} else {
out("Really? You though you could survive with those supplies?");
}
break;
case "MOUNTAIN":
if ((choiceBank[0] === 'CAMP' || choiceBank[0] === 'FIRE STARTER') && choiceBank[1] === 'STUDDED' && choiceBank[2] === 'WOLF') {
out("Wow! You're a survival expert!");
} else {
out("Really? You though you could survive with those supplies?");
}
break;
case "SEA":
if (choiceBank[0] === 'OXYGEN TANK' && choiceBank[1] === 'SWIMFIN' && choiceBank[2] === 'WHALE') {
out("Congratulations on choosing well, you survived to live another day!");
} else {
out("Really? You though you could survive with those supplies?");
}
break;
default:
out("Sorry, one of the responses was invalid, please try again.");
}
function out(s) {
var node = document.createElement('div');
node.innerHTML = s + '<br>';
document.getElementById('out').appendChild(node);
}
<div id="out"></div>