Why does it always end in false?


#1


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


How do I stop it from printing false at the end? It runs through the loop until the condition is false, just as it should, but then it prints 'false,' which it shouldn't. It's a useless piece of information that is immersion breaking if you're building a game, and just plain bad in any other case. For example, the following will print something about you either damaging the dragon or dying until either you win or die. Then it prints false.


while (slaying) {
  if (youHit) {
    console.log("You hit the dragon and did " + damageThisRound + " damage!");
    totalDamage += damageThisRound;
    
    if (totalDamage >= 4) {
      console.log("You did it! You slew the dragon!");
      slaying = false;
    } else {
      youHit = Math.floor(Math.random() * 2);
    }
  } else {
    console.log("The dragon burninates you! You're toast.");
    slaying = false;
  }
}


#2

this problem is caused by the console, because you update a variable after the last console.log()

you could simply print an empty string at the end, to ensure false isn't printed

its nothing to worry about, the console has been echoing the last update variable throughout the course, its nothing new


#3

Why does it do that? Is that a problem with codecademy or JS in general?


#4

some consoles have this behavior (including codecademy). Its not that relevant even, consoles are generally only used for debugging. Output is often displayed html webpages.


#5

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