Dragon slayer .The 2nd if statement


#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 did" +" "+ totalDamage +"!");
        totalDamage += damageThisRound;
        if(totalDamage >=4){
            console.log("You slew the dragon");
            slaying=false;
        } else {
            youHit= Math.floor(Math.random() * 2);
        }
    } else {
        console.log("you died cause u missed the attack!");
        slaying = false;
    } 
}

At first , i press save and submit code a few time to check some results. And one of the results :
You did 0 ! .. why it could be a "0" damage with this Math.floor(Math.random()* 5 + 1 ).
i even put the "math.floor" seperate to check . always show 1-5.
but together in this whole code. You did 0! =-=


#2

Hi @phucvuong,

You are doing:

console.log("you did" +" "+ totalDamage +"!");

So, it is logging the value of variable totalDamage, which is obviously 0.

You might want to log damageThisRound instead. :wink:

Hope it helps! :smiley:


#3

wow. okay. i was stupid hahahahahh . thanks mate