Game appears to be playing correctly, but console window logs false at the end of game


#1


https://www.codecademy.com/courses/javascript-beginner-en-mrTNH-6VIZ9/0/6?curriculum_id=506324b3a7dffd00020bf661


The code appears to work and the game plays correctly, but the console window always shows "false" at the end. What am I doing wrong with my assigning false to slaying, it appears to be in the right loops.

---Console window begin---

Congrats, dragon took damage!
Congrats, dragon took damage!
The dragon has been defeated!
false

---Console window end---


var slaying = true;
var youHit = Math.floor(Math.random()*1);
var damageThisRound = Math.floor(Math.random()*5+1);
var totalDamage = 0;

while (slaying) 
{
 if (youHit) 
 {
    console.log("Congrats, dragon took damage!");
    totalDamage +=damageThisRound;
    if (totalDamage >= 4) 
    {
     console.log("The dragon has been defeated!");
     slaying = false;
    }
    else
    {
      youHit = Math.floor(Math.random()*2);  
    }
 }
 else
 {
    console.log("You've been defeated by the dragon..."); 
    slaying = false;   
 }  
}


#2

you update a variable after the console.log, causing false to be logged.

swap slaying = false and console.log around


#3

Unless i switched them incorrectly, doesnt this make the randomized number appear on the log instead of false? It also twisted some answers?


#4

it shouldn't, given there are no variable updates after last console.log (following program flow)


#5

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