Dragon Slayer where is the syntax error in my code (Some one pl help)


#1

Below is the code

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

while (slaying)

if (totalDamage >= 4) {
  console.log("You did it! You slew the dragon!");
  slaying = false;
} else {
console.log("yer dead.");
slaying = false;

}
}


#2

Hi
didn't you forget the opening { just after the while ?

Cheerio


#3

Hmm, there seems to be a lot more wrong with this code. Here is the corrected, full version:


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

while(slaying) {
   slaying = false;
       if(youHit){
           console.log("You hit!");
           totalDamage += damageThisRound;
       if (totalDamage >= 4) {
           console.log("You win!");
           slaying = false;
           }
      else {
           youHit = Math.floor(Math.random() * 2);
           }
       }
       else{
          console.log("You missed!")
          slaying = false;
       }
}