5. The second "if" statement



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;


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


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...


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.


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