Error Message: Oops, try again. It looks like you didn't set slaying to true


#1



Please check my code. When I try to run the code I get the error message "Oops, try again. It looks like you didn't set slaying to true". Please tell me what adjustment I need to make to the code to make it run correctly. Thank you in advance for your help.


var slaying = true;
var youHit = Math.floor(Math.random() * 2);
var damageThisRound = (Math.random()*5 + 1);
var totalDamage = 0;
while(slaying) {
    if (youHit) {
        console.log("You hit the dragon!");
        totalDamage += damageThisRound;
        if (totalDamage >= 4) {
              console.log("You won!");
              slaying = false;
          } else {
              youHit = Math.floor(Math.random() * 2);
          }
  } else {
      console.log("You were killed by the dragon!");
      slaying = false;
  }
}


#2

You linked to lesson 2, what lesson are you on?


#3

I am on lesson 'Declare your variables'.


#4

You just need this:

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

#5

Thank you for your response and help. I replaced my code with your code but I am still getting the same error message. At this point, I am going to try using another browser and try to determine if it may be a browser issue. I'll give you an update later.

Thanks again for your help.


#6

can we seen updated version of your code?

i hope you removed this:

while(slaying) {
    if (youHit) {
        console.log("You hit the dragon!");
        totalDamage += damageThisRound;
        if (totalDamage >= 4) {
              console.log("You won!");
              slaying = false;
          } else {
              youHit = Math.floor(Math.random() * 2);
          }
  } else {
      console.log("You were killed by the dragon!");
      slaying = false;
  }
}

this will set slaying to false, causing the error message.


#7

Thank you for your help. The issue has been resolved.

Have a good one.


#8