Dragon Slaying Not Working


#1

//variables
var slaying = true;
var youHit = Math.floor(Math.random() * 2);
var damageThisRound = Math.floor(Math.random() * 5 + 1);
var totalDamage = 0;

//script
while(slaying) {
if(youHit = 1) {
console.log("You Hit The Dragon!");
totalDamage += damageThisRound;
if(totalDamage >= 4) {
console.log("You Killed The Dragon");
slaying = false;
}
}
else if (youHit = 0) {
console.log("You Missed!");
}

};

Script always makes me win, can anyone tell me why? The site still says it's correct.


#2

The right code: :slight_smile:

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("You Win");
                slaying = false;
            } else { 
                youHit = Math.floor(Math.random() * 2);
            }
    } else {
        console.log("You missed");
        slaying = false;
    }
};

#3

Thank you, this made me understand my mistake.


#4

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