5. The second "if" statement


#1


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


SyntaxError: Unexpected token else


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


#2

Well just at first glance it would seem you have 4 } but 5 { try adding another } or deleting a { to get to that even number of brackets


#3

Thanks, tried deleting one, tried adding one, still got the same error.

I compared to what the HINT had, and still couldn't find the difference!! I cheated and copy and pasted the HINT code and it worked... I know, real cheap...


#4

Well, sometimes it is necessary. I would say look more into what the differences were between the hint code and your previous code so as to learn from this experience but I wouldn't be to hard on yourself. I know I have done it before and others have as well. Obviously we just don't want it to become a habit but every once in a while is fine.


#5

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