5/6 Dragon Slayer. My question is why do we need to put } else { youHit = Math.floor(Math.random() * 2); } second time in there? I don't understand it


#1

5/6 My question is why do we need to put:

} else {
youHit = Math.floor(Math.random() * 2); }

second time in there (in my code)? I don't understand it.

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 lose!");
slaying = false;
}
}


#2

What is your doubt or question? I don't understand. Can you be more specific?


#3

@bekniyazov it is provided with the youHit value..

because as suppose after save n submit if u will again use it, if u haven't given youHit a new number(What has been done) u will find it starts from same youHit value with which u have ended. Thus resulting in same result often.

so it's been provided with new youHit value so u get differ result each tyme.


#4

Pretty close, it's not about another run of the program but about another run of the loop. YouHit is a coinFlip and you flip the coin first to see if you hit at all and you flip it again for each new try to hit the dragon otherwise you'd always hit when you hit on the first try.


#5

@haxor789 thanks buddy


#6

@haxor789 So is it like a second try to hit dragon if you did not kill him yet?
And does it mean that if second youHit will be equal to the true, then the whole code will run again?