6/6. Why is "false" printing at the end?


#1

Here is my code, it passed and seems to work but the at the end of the game it always prints out "false". Why is that if I'm not calling the console log on false?

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 for " + damageThisRound + " damage!");
        totalDamage+=damageThisRound;
        if (totalDamage>=4){
            console.log("You killed the damn thing!");
            slaying = false;
        }
        else {
            youHit = Math.floor(Math.random() * 2);
        }
    }
    else{
        console.log("The dragon chomped you up");
    slaying=false
    }
}

#2

Hi @blogplayer70329,

don't get too caught up on this, really it is just a little quirk of the console in use, and the false you are seeing it actually the slaying as it is being set.
You can test this by setting a fake variable to true under your slaying and watching that print out instead, because that is the last action the program takes. An example follows

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 for " + damageThisRound + " damage!");
        totalDamage+=damageThisRound;
        if (totalDamage>=4){
            console.log("You killed the damn thing!");
            slaying = false;
            test = true; //now you will see true display
        }
        else {
            youHit = Math.floor(Math.random() * 2);
        }
    }
    else{
        console.log("The dragon chomped you up");
        slaying=false;
        test = true; //now you will see true display
    }
}