Skip to content Skip to sidebar Skip to footer

My Function Is Broken It Seems Javascript

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

Solution 1:

Try if (battle === "Flee" || battle === "flee") The right hand side is just evaluating the string "flee" for truthiness, which always evaluates true.


Solution 2:

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

Solution 3:

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);
}

Post a Comment for "My Function Is Broken It Seems Javascript"