Slaying value displayed when adding after else


#1


adding "slaying = false;" to the end of the while loop will cause a display error

It does not follow the procedure and thus does not cause a display error:
while(slaying) {
if (youHit) {
console.log("You hit!");
} else {
console.log("You missed!");
}
slaying = false;
}

This is the faulty default code.
It will cause "true" or "false" (without quotations, as boleans) to appear below the console.log messages on screen.

I can't delete this post. So before anybody responds, let me add an unrelated actual question:
What causes a string to be displayed with added quotations? Referencing it in a more narrow scope when it's undefined? I cleaned up my while do while code (the review, free form exercise) and now I forgot how I caused it.


var slaying = true
var youHit = Math.floor(Math.random() * 2)
var damageThisRound = Math.floor(Math.random() * 5 + 1)
var totalDamage = 0
while(slaying) {
    console.log("You hit the dragon for " + youHit + " points!");
    slaying = false;
    if(youHit) {
//        slaying = false;
        console.log("You killed it!");
        } else  {  
//    slaying = false;
    console.log("Dragon killed you!");
    }
}


#2

because the codecademy console displays the last value to the console? It is a useful feature, i don't understand why so many people worry about it. You can also set slaying to false much earlier, the the last thing is a console.log statement, causing your boolean value not to be displayed


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.