5. The second "if" statement syntax error


#1

It looks right to me but it says syntax error. Would appreciate if someone pointed out my mistake.
here is my 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 (youHit) {
console.log("Good job! You hit the Dragon!");
totalDamage += damageThisRound; }
if (totalDamage >= 4) {
console.log("You have slayed the dragon!");
slaying = false;
} else {
youHit = Math.floor(Math.random() * 2);
}
} else {
console.log("You've missed the Dragon and died! GAME OVER!") ;
slaying = false;
}
}

this is the part where i think my mistake is:

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


#2

@neseballe,

With your

if (youHit) {
 console.log("Good job! You hit the Dragon!");
 totalDamage += damageThisRound; }

you have closed the so-called IF code-block.
The Javascript interpreter will now only accept
a new IF-statement or an ELSE statement.

In this exercise you will want to end-up with a code-structure

while ( condition ) {
    if (condition ) {
         //
       if (condition ) {
           //
       }
       else {
          //
       }
    }
    else {
         //
    }
 }

#3

Thank you! This solved it.