Interesting


#1

I used this code for about 30 times and the dragon have no chance agains me :slightly_smiling:

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 hit the dragon!");
        totalDamage += damageThisRound;
            if(totalDamage >= 4) {
                console.log("The dragon is dead!");
                slaying = false;
            } else {
            youHit= Math.floor(Math.random() * 2);
            }
    } else {
        console.log("The dragon defeated you!");
        slaying = false;
    }
}

is something wrong with the code?


#2

Hi!
I'm new to programming too...but I could notice you have this error :

if(youHit = 1)

You are assigning the value of 1 to youHit. The "=" assigns some value (in your case 1) to the variable (youHit).
From what i have learned until now, the if statement should verify IF(this happens) do this {slay the dragon or whatever...}.
Try to change the "=" which is assigning, with "===" which checks if something is equal to something else .
I really hope i could help you with my little knowledge .


#3

Thx that one was the solving of the problem!