5. Logical Operators - SyntaxError: Unexpected token else



I was trying to do the exercise for "Code Your Own Adventure 2!", and I got pretty far in it (A bit overboard, I know.) The problem is that I keep getting a syntax error saying that I put an unexpected else statement, at least I think that's what it means, but I haven't even put an else statement in the code! If anyone can help me with this, I would really appreciate it. Thank you :smiley:

var user = prompt("You are walking through the streets of a dangerous neighborhood with your girlfriend. Suddenly, as you pass a dark alley, an armed man jumps out, grabs your girlfriend, and asked for your money. Do you PAY him, ATTACK him, PERSUADE him, or RUN from him?").toLowerCase();

switch(user) {
    case 'pay':
        var cash = prompt("Do you have any money on you?").toLowerCase();
        var rich = prompt("Do you hae a lot of money on you?").toLowerCase();
            if(cash === yes || rich === "yes") {
                console.log("The robber is happy that he gets your money. He lets you both go, then runs away.");
    case 'attack':
        var quick = prompt("Are your reflexes quick?").toLowerCase();
        var armed = prompt("Are you armed and dangerous?").toLowerCase();
            if(quick === "yes" && armed === "yes") {
                console.log("You shoot the mugger before he even has the chance to think, he never stood a chance. You and your girlfriend escape unharmed."); 
            } else if(quick === "yes" && armed === "no") {
                var decision = prompt("You jump at the robber and he draws his gun. He just misses you, then you tackle him and take his gun from him. Do you KILL or SPARE him?").toLowerCase();
                switch(decision) {
                    case 'kill':
                        console.log("You kill the robber, and you both manage to get away.");
                    case 'spare':
                        console.log("The robber runs away, but who's to say that he won't come back?")
                    console.log("You took too long to think of a right decision and the robber stabs you.");
    case 'run':
        var run = prompt("Can you run fast?").toLowerCase();
            if(run === "yes") {
                console.log("The robber shoots at you, but he's too slow. He kills your girlfriend instead, then flees the scene.");
            } else if(run === "no") {
                console.log("The robber shoots you in the back and kills you. Then he kills your girlfriend because she's a witness.");
    case 'persuade':
        var charisma = prompt("Are you charismatic?").toLowerCase();
        var luck = prompt("Are you lucky?").toLowerCase();
            if (charisma === "yes" && luck === "no") {
                var decision2 = prompt("You successfully persuade the robber to let your girlfriend go, but then he points his gun to you! What rotten luck! What do you do now, TALK your way out of it, or FIGHT back?").toLowerCase();
                    switch(decision2) {
                        case 'talk':
                            console.log("Your silver tongue wins this battle, you both get away unscathed.");
                        case 'fight':
                            console.log("You've always been more of a talker than a fighter. The robber calls your bluff, and he charges at you with his gun and kills you.");
            } else if (charisma === "no" && luck === "yes"); {
                console.log("Your attempt to dissuade the robber didn't work, but your distraction gave your girlfriend enough time to steal his gun from him! Now the tables have turned, and you both get away.");
            } else if (charisma === "no" && luck === "no"); {
                console.log("Congratulations, you just managed to tick the robber off more. Now you're both dead.");
    console.log("That isn't one of the choices, try again.");


Hi there @colecj98,

Ain't nothing wrong with being ambitious, hahaha. Anyway, I tried searching your error on google to see if there was a shortcut to finding the answer (other than if there's an else, of course) and I came across this post from the old forum. It's from a different exercise, but maybe it could apply to your program, too? At least that's what I've gathered from my own test :wink:

And if you haven't seen it yet, perhaps this reference would be useful to you. Not that I think you deliberately made an error here; they're probably typos, but as a fellow JS nub I've found it useful to keep open in the background to refer to from time to time. :slight_smile:



A post was split to a new topic: 5. Logical Operators - Unexpected token