Not logging slain


#1

Ran my code a few times, and it switches between hit and not, however my code won't log the dragon is slain message. Am I missing something?

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("Grats!  You hit the giant lizard!");
        totalDamage+=damageThisRound;
        if(totalDamage>=4){
            console.log("The beast has been slain!  Party hard.");
            }
        else{
            youHit=Math.floor(Math.random() * 2);
            }
        }
    else{
        console.log("Sorry, looks like your well-done... more like charcol really.");
        }
    slaying=false;
    }


#2

Maybe if you had visited what you'll be building, you could have figured that out. That said, you're missing one curly bracket at the end for the while.

See:

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 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;
  }
}

#3

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