Dragon slaying not set to true?


#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)
{console.log("You hit the dragon!!!");
totalDamage += damageThisRound;
if(totalDamage >= 4)
{console.log("You slew the dragon!!!");
slaying = false;}
else{youHit = Math.floor(Math.random() * 2);}}
else{console.log("A swing and a miss...and now the dragon is pissssed!!");
slaying = false;}}

Borrowed this code from another post... and I have borrowed many more functional codes, but I always get the same error. "Oops, try again. It looks like you didn't set slaying to true." Why?


#2

The reason youHit = Math.floor(Math.random() * 2) is so it will either equal 1 or 0. Now in your if(youHit) nothing is going on perhaps it needs to check if its equal to something? if(youHit === ?) Now if it was then you would hit the dragon but if it wasn't the dragon would not be happy. The else{youHit = Math.floor(Math.random() * 2);}} is unnecessary so you can get rid of it and just have the else{console.log("A swing and a miss...and now the dragon is pissssed!!");
slaying = false;}}