Can't see the fault


#1

This is the code i am using, and i cant see where i am going wrong. please help.
(sorry for the bad englis. i am not a native speaker)


var slaying = true;
var you = Math.floor(Math.random()*2);
var damageThisRound = Math.floor(Math.random()*5 + 1)
var totalDamage = 0
while(slaying) {
    if (you) {
        console.log("You hit!");
        totalDamage=+damageThisRound
        if (totalDamage>=4) {
            console.log("Dragon has fallen, now get the princess!.");
            slaying=true;
        }else{
            youHit=Math.floor(Math.random()*2);
        }
    } else {
        console.log("You missed!");
    }
    slaying = false;

}


this is the new code
var slaying = true;
var youHit=Math.floor(Math.random()*2);
var damageThisRound = Math.floor(Math.random()*5 + 1)
var totalDamage = 0
while(slaying) {
    if (youHit) {
        console.log("You hit!");
        totalDamage =+ damageThisRound
        if (totalDamage>=4) {
            console.log("Dragon has fallen, now get the princess!.");
            slaying=true;
        }else{
            youHit=Math.floor(Math.random()*2);
        }
    } else {
        console.log("You missed! The dragon ate you");
    }
    slaying = false;

}


#2

totalDamage=+damageThisRound check this line first.

Also,

console.log("Dragon has fallen, now get the princess!.");
            slaying=true;

If the dragon has fallen, the game stops. Are you sure you want to keep slaying?

And,

youHit=Math.floor(Math.random()*2);

Now your variable is youHit, but previously was "you". "you" is pretty vague, I'd have set youHit from the beginning.

Last,

    } else {
        console.log("You missed!");
    }

Are you sure that the dragon hasn't killed you by then?


#3

oke saw wat i did wrong there. but i still get the mesagge that slaying is set to true.

and yes the dragon would have killed me bij then :slight_smile:


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.