Reputation: 3
Basically no matter what I enter when I call the function it says you have fled. I may just be overlooking something but I can't figure out what.
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");
}
}
Upvotes: 0
Views: 89
Reputation: 2674
Try:
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);
}
Upvotes: 0
Reputation: 11144
if (battle === "Flee" || "flee")
in this if condition
you should write
if (battle === "Flee" || battle === "flee")
if you want to ignore case then batter if u change string to lower case here is example
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");
}
}
Upvotes: 2
Reputation: 741
Try if (battle === "Flee" || battle === "flee")
The right hand side is just evaluating the string "flee" for truthiness, which always evaluates true
.
Upvotes: 2