Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was coding a "slay your own dragon game," it is quite simple, just by looking at the code i'm about to show you will give you an idea of what it is about. Anyways, I got "SyntaxError: Unexpected token else" as I was programming. I tried to solve it, but I didn't know where I went wrong :(. Maybe you guys can help me.
Heres the code:

C#
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("The dragon defeated you");
        slaying = false;
    }
}


I'm am not the best coder(obviously), so can you make your answer simple just for me? Thanks! I appreciate your help!
Posted
Comments
UWStudents 25-Jan-15 8:48am    
I just realized that my while loop didn't have an ending "}," thats probably the problem, but if you guys could, check my code over for any other problems! Thanks again

Formatting your code with the right indentation can help you a lot...
See the first else - it belongs the the inner if, but there is no closing } after it so the second else is pair-less...
C#
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)
        }
    // here you missign a closing }
    else {
        console.log("The dragon defeated you");
        slaying = false;
    }
}
 
Share this answer
 
Glad to see you found the issue. Syntax errors usually are pretty easy to find if your development environment points to a specific line of code. If you are using Visual Studio you can use the menu command "Format Document" and that will sometimes tell you which line is wrong but it will also only format the document when all the syntax is correct.

In Visual Studio, you'll get a red squiggly line usually under the bad syntax part as well.
 
Share this answer
 
Yeah, I think this code is the proper way, just need to add one more "}"

Check it out:
C#
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 for" + " " + totalDamage);
        totalDamage + damageThisRound;

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

}
 
Share this answer
 
Comments
PIEBALDconsult 3-Apr-15 19:08pm    
Do not answer your own questions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900