youHit vs. youHIT = Math.floor(Math.random() * 2)


#1

In the second if statement, what is the difference between just using youHit versus youHIT = Math.floor(Math.random() * 2)?

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 === 1) {
        console.log("You just hit the dragon with " + damageThisRound + " damage.");
        totalDamage += damageThisRound;
        if (totalDamage >= 4) {
            console.log("The dragon has been defeated! Congratulations brave Knight!");
            slaying = false;
        } else {
            youHit = Math.floor(Math.random() * 2);
            //or using just youHit
        }
    } else {
        console.log("The dragon incinerates you! You are defeated!");
        slaying = false;
    }
}

I feel like since youHit is an already defined global variable, I wouldn't have to redefine it in the second if statement. I'm a novice, so I'm probably wrong. Please help me understand this better. Thanks!


#2

you want to redefine youHit, if you don't it means if the first hit was a hit, all the followings hits will be hits as well, while if you redefine there is another 50% change to hit and a 50% to miss


#3

That's exactly the answer I needed. Now I understand why. Thanks so much!