Dragon slayer in JS . > 5


#1


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


In the last else it says " expected an identifier and instead saw 'else' . Missing ; before statement" Help please ( It says Unexpected token else in the console)


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!!")
    }
    totalDamage+=damageThisRound;
   if ( totalDamage >= 4 ){console.log ( " You slew the dragon!") 
     slaying = false;
  } else  {
        youHit = Math.floor(Math.random() *2);
    
  }
    }  else {
            console.log ( " Boo! The dragon burned you to ashes");
            slaying =false 
        }


#2

Check your console.log() calls. Are they formatted right?


#3

Remove ; before { and try again.
Please, format your code. Is it your full code?

Updated
This part:

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

should be inside of the first if statement:

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

Please, do all correction.


#4

It doesn't matter in this


#5

Thanks! Got it ! It was an error with the braces


#6

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