6/6 kill decided after first hit


#1

var slaying = true;
var youHit = Math.floor(Math.random() * 50);
var d = Math.floor(Math.random() * 20 + 1);
var totalDamage = 0;
while (slaying) {
if (youHit >= 10) {
console.log("you hit the dragon for "+d+" and did "+totalDamage+" altogether");
totalDamage += d;
if (totalDamage >= 9999) {
console.log("that was only a LVL 1 dragon so... good job?")
slaying = false;
} else {
youhit = Math.floor(Math.random() * 50);
d = Math.floor(Math.random() * 20 + 1);
}
} else {
console.log("you died");
slaying = false;
}
}

This code works and I modified it so you hit for a different amount of damage each time, but if you get 1 hit on the dragon you automatically win, why is this happening and how can I fix it.


#2

@rednazer Not really your code works....


#3

see that's what I mean if you get 1 hit you win, for instance if you "hit the dragon for 3" then you NEVER will lose the game. I tried so many times and it is either you get killed first hit or you kill the first hit.