基本上无论我在调用函数时输入的是什么,它都说你已经逃离了。我可能只是忽略了一些东西,但我无法弄清楚是什么。
function attack() {
var battle = prompt("Attack of Flee?")
if (battle === "Flee" || "flee") {
alert("You have fled");
}
else if (battle === "Attack" || "attack"){
alert("You have attacked");
}
}
答案 0 :(得分:2)
试试if (battle === "Flee" || battle === "flee")
右边只是评估字符串“逃离”的真实性,它总是评估true
。
答案 1 :(得分:2)
if (battle === "Flee" || "flee")
你应该写
if (battle === "Flee" || battle === "flee")
如果您想忽略大小写,那么如果您将字符串更改为小写,则会打击 这是示例
function attack() {
var battle = prompt("Attack of Flee?");
if (battle.toLowerCase() === "flee") {
alert("You have fled");
} else if (battle.toLowerCase() === "attack") {
alert("You have attacked");
}
}
答案 2 :(得分:0)
尝试:
function attack() {
var battle = prompt("Attack of Flee?");
var ActionTaken = "";
if (battle.ToLowerCase() == "flee") {
ActionTaken = "You have fled";
}
else if (battle.ToLowerCase() == "attack") {
ActionTaken = "You have attacked";
}
alert(ActionTaken);
}