8/6. Only winning?


#1

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 kicked the ■■■ of dragon and did " + damageThisRound + " damage!");
totalDamage += damageThisRound;
if (totalDamage >= 4){
console.log("You kicked his ■■■ real bad");
slaying = false;
} else {
youHit = Math.floor(Math.random()*2);
}
} else {
console.log("The dragon kicked your ■■■");
slaying = false;
}
}

Above is the code i wrote. I don't have any problem regarding the tutorials but there is one thing that i cant understand is i am doing damage all the time. I mean that im not losing so there has to be some problem in my code as to why it is never getting 0 in youHit.


#2

I think it's because you wrote if (youHit = 1) when you declared your first if/else statement. Try running it just as if(youHit), or alternatively as if (youHit === 1) a couple times and see if you lose.


#3

Thanks. I ran the code with (youHit === 1). Now its working fine!