Click here to Skip to main content
15,884,739 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I keep getting the error "SyntaxError: Unexpected token case". I don't know what I am doing wrong. I could not narrow the code down even though I tried.




C#
var user = prompt ("Are you a goblin, knight, troll, human, or wizard?").toLowerCase()
var name = prompt("What is your name?").toLowerCase()
var gender = prompt("Are you male or female?").toLowerCase()
switch(user) {
        case "goblin":
            console.log("Hello, you are going to have a fun journey as a goblin");
            var meetAnt = prompt ("You run into a giant ant eater, you can attack or run. Do you choose to attack?").toLowerCase()
            if(meetAnt == "yes") {
                console.log ("you stab him in the eye, but then he hits you with his trunk. You are injured");
                var finishAnt = prompt("you can either risk your life and finish him, or try to run away. Do you finish him?").toLowerCase()
                if(finishAnt == "yes") {
                    console.log("congratulations you have killed the aint eater!")   
                }
                else if(finishAnt == "no") {
                    console.log("You run away and barley make it, you are badly hurt");
                    } else {
                        console.log("sorry thats not an option")
                    }
            }
            else if(meetAnt == "no") {
                console.log("You run past the ant eater and get to saftey. You are very lucky")
                
            }
            else {
                console.log("Sorry that is not an option.")
            break;
        case "knight":
            console.log("Hello, you are going to have a fun journey as a knight");
            var dayFight = prompt("You are versing the goblins in a fight, you need to choose how many days you go to fight(1-100). Remember if you choose too little days then you won't have stayed long enough to win the fight but if you stay to long you have more of a chance of dying or getting injured").toLowerCase()
            if(dayFight = parseInt(dayFight, 10); {
                console.log("You did not fight enough days, which has resulted in your kingdome loosing the war to the goblins.");
            }
            else if(dayFight > 60) {
                console.log("You have went to war to long and been seriously injured.");
            }
            else if(40 <= dayFight && dayFight <= 60) {
                console.log("You have been at war for the right amount of time. You have came out of the war with no injuries and you also have defieted the goblins!")
            }
            break;
        case "troll"):
            console.log("Hello, you are going to have a fun journey as a troll");
            var red = true;
            var green = true;
            var yellow = false;
            var blue = false;
            var houseRaid = prompt("You see four huts in the middle of the woods. You can raid two. There is a red hut, blue hut, yellow hut, and green hut. What is one color house that you want to raid?").toLowerCase()
            var doorPick = prompt("Do you want to enter throuhg the back door or front door?").toLowerCase()
            if(doorPick||houseRaid == "red"||"green" && "back door") {
                console.log("You raided these houses and left before any villagers would see you");
            }
            else {
                console.log("You raided those houses, but one of them was a booby trap and you are now captured");
            }
            break;
        case "human":
            console.log("Hello, you are going to have a fun journey as a human");
            var reinforceFound = prompt("You know a storm is comming and you have to reinforce your hut, but you only have enough material to reinforce either your lower foundations or higher foundations. Which do you inforce? Higher or lower?").toLowerCase()
            if(reinforceFound == "lower") {
                console.log("The storms winds pushed down the top of your house and caved in your roof.");
            }
            else if(reinforceFound == "higher") {
                console.log("The storm did not do that much damage to your house due to your reinforced higher foundations. Good choice");
            }
            else {
                console.log("sorry but that is not an option. Pick either 'higher', or 'lower'")
            }
            break;
        case "wizard"):
            console.log("Hello, you are going to have a fun journey as a wizard");
            var blood = true;
            var dust = true;
            var wings = false;
            var mushrooms = false;
            var postion = prompt("You are working on a new healing potion but you do not know what you need to add to finish it. You have 4 ingrediants; blood, dust, wings, mushrooms. Pick one too add. (WARNING: Pick carefully because if you choose the wrong ingerdiant, then your potion will be ruined.)").toLowerCase()
            if(postion == "wings" || postion == "mushroom") {
                console.log("You picked a bad ingrediant and now your potion is ruined.");
            }
            else if(postion = "dust" || "blood") {
                console.log("you picked the right ingrediant and your potion is okay")
            }
            else {
                console.log("sorry but that is not an option");
            }
            break;
        default:
            console.log("Sorry but that is not a character in the game");
};
Posted
Updated 22-Mar-15 20:22pm
v2

Looks like you are a bit stingy with the semicolons.
Try ending each statement with ;
Java
var user = prompt ("Are you a goblin, knight, troll, human, or wizard?").toLowerCase();
var name = prompt("What is your name?").toLowerCase();
var gender = prompt("Are you male or female?").toLowerCase();
etc. etc.
 
Share this answer
 
v2
Quite a number of typo,
for example = instead of ==, ; instead of ) etc, got it fixed:
C#
var user = prompt ("Are you a goblin, knight, troll, human, or wizard?").toLowerCase()
var name = prompt("What is your name?").toLowerCase()
var gender = prompt("Are you male or female?").toLowerCase()
switch(user) {
        case "goblin":
            console.log("Hello, you are going to have a fun journey as a goblin");
            var meetAnt = prompt ("You run into a giant ant eater, you can attack or run. Do you choose to attack?").toLowerCase()
            if(meetAnt == "yes") {
                console.log ("you stab him in the eye, but then he hits you with his trunk. You are injured");
                var finishAnt = prompt("you can either risk your life and finish him, or try to run away. Do you finish him?").toLowerCase()
                if(finishAnt == "yes") {
                    console.log("congratulations you have killed the aint eater!")
                }
                else if(finishAnt == "no") {
                    console.log("You run away and barley make it, you are badly hurt");
                    } else {
                        console.log("sorry thats not an option")
                    }
            }
            else if(meetAnt == "no") {
                console.log("You run past the ant eater and get to saftey. You are very lucky")

            }
            else {
                console.log("Sorry that is not an option.")
            }
            break;
        case "knight":
            console.log("Hello, you are going to have a fun journey as a knight");
            var dayFight = prompt("You are versing the goblins in a fight, you need to choose how many days you go to fight(1-100). Remember if you choose too little days then you won't have stayed long enough to win the fight but if you stay to long you have more of a chance of dying or getting injured").toLowerCase()
            if(dayFight == parseInt(dayFight, 10)) {
                console.log("You did not fight enough days, which has resulted in your kingdome loosing the war to the goblins.");
            }
            else if(dayFight > 60) {
                console.log("You have went to war to long and been seriously injured.");
            }
            else if(40 <= dayFight && dayFight <= 60) {
                console.log("You have been at war for the right amount of time. You have came out of the war with no injuries and you also have defieted the goblins!")
            }
            break;
        case "troll":
            console.log("Hello, you are going to have a fun journey as a troll");
            var red = true;
            var green = true;
            var yellow = false;
            var blue = false;
            var houseRaid = prompt("You see four huts in the middle of the woods. You can raid two. There is a red hut, blue hut, yellow hut, and green hut. What is one color house that you want to raid?").toLowerCase()
            var doorPick = prompt("Do you want to enter throuhg the back door or front door?").toLowerCase()
            if(doorPick||houseRaid == "red"||"green" && "back door") {
                console.log("You raided these houses and left before any villagers would see you");
            }
            else {
                console.log("You raided those houses, but one of them was a booby trap and you are now captured");
            }
            break;
        case "human":
            console.log("Hello, you are going to have a fun journey as a human");
            var reinforceFound = prompt("You know a storm is comming and you have to reinforce your hut, but you only have enough material to reinforce either your lower foundations or higher foundations. Which do you inforce? Higher or lower?").toLowerCase()
            if(reinforceFound == "lower") {
                console.log("The storms winds pushed down the top of your house and caved in your roof.");
            }
            else if(reinforceFound == "higher") {
                console.log("The storm did not do that much damage to your house due to your reinforced higher foundations. Good choice");
            }
            else {
                console.log("sorry but that is not an option. Pick either 'higher', or 'lower'")
            }
            break;
        case "wizard":
            console.log("Hello, you are going to have a fun journey as a wizard");
            var blood = true;
            var dust = true;
            var wings = false;
            var mushrooms = false;
            var postion = prompt("You are working on a new healing potion but you do not know what you need to add to finish it. You have 4 ingrediants; blood, dust, wings, mushrooms. Pick one too add. (WARNING: Pick carefully because if you choose the wrong ingerdiant, then your potion will be ruined.)").toLowerCase()
            if(postion == "wings" || postion == "mushroom") {
                console.log("You picked a bad ingrediant and now your potion is ruined.");
            }
            else if(postion = "dust" || "blood") {
                console.log("you picked the right ingrediant and your potion is okay")
            }
            else {
                console.log("sorry but that is not an option");
            }
            break;
        default:
            console.log("Sorry but that is not a character in the game");

};
 
Share this answer
 
v2
Hi,
I found these ; & ) & } are missing or extra at various places.
I made required correction, because i found you are a beginner in javascript (may be in programming also).
When i started programming few years back i also do same mistakes.
For a beginner if code runs "I got victory on Everest" :) and if doesn't runs "Oh god please send some to help me" :(

DO Remember just struggle with your code to find errors, so that you can learn and help some else.

i want to give one advise to you, to avoid such type errors. Write code step by step. for example.

JavaScript
function callfun() {}


then
JavaScript
    function callfun() {
        var user = prompt("Are you a goblin, knight, troll, human, or wizard?").toLowerCase();
        var name = prompt("What is your name?").toLowerCase();
        var gender = prompt("Are you male or female?").toLowerCase();
}


then

JavaScript
    function callfun() {
        var user = prompt("Are you a goblin, knight, troll, human, or wizard?").toLowerCase();
        var name = prompt("What is your name?").toLowerCase();
        var gender = prompt("Are you male or female?").toLowerCase();

        switch (user) {
             break;
       }
}


then

JavaScript
function callfun() {
        var user = prompt("Are you a goblin, knight, troll, human, or wizard?").toLowerCase();
        var name = prompt("What is your name?").toLowerCase();
        var gender = prompt("Are you male or female?").toLowerCase();

        switch (user) {

            case "goblin":
                console.log("Hello, you are going to have a fun journey as a goblin");
                var meetAnt = prompt("You run into a giant ant eater, you can attack or run. Do you choose to attack?").toLowerCase();
                if (meetAnt == "yes") {}
                else if (meetAnt == "no") {}
                else {}
            break;
        }
}


then

JavaScript
if (meetAnt == "yes") {
    console.log("you stab him in the eye, but then he hits you with his trunk. You are injured");
    var finishAnt = prompt("you can either risk your life and finish him, or try to run away. Do you finish him?").toLowerCase();
    if (finishAnt == "yes") {
    }
    else if (finishAnt == "no") {
    }
    else {
    }
}
else if (meetAnt == "no") {
}
else {
    console.log("Sorry that is not an option.")
}


next is

JavaScript
if (meetAnt == "yes") {
    console.log("you stab him in the eye, but then he hits you with his trunk. You are injured");
    var finishAnt = prompt("you can either risk your life and finish him, or try to run away. Do you finish him?").toLowerCase();
    if (finishAnt == "yes") {
        console.log("congratulations you have killed the aint eater!");
    }
    else if (finishAnt == "no") {
        console.log("You run away and barley make it, you are badly hurt");
    }
    else {
        console.log("sorry thats not an option");
    }
}
else if (meetAnt == "no") {
    console.log("You run past the ant eater and get to saftey. You are very lucky")
}
else {
    console.log("Sorry that is not an option.")
}


Finally your code is here

JavaScript
function callfun() {
        var user = prompt("Are you a goblin, knight, troll, human, or wizard?").toLowerCase();
        var name = prompt("What is your name?").toLowerCase();
        var gender = prompt("Are you male or female?").toLowerCase();

        switch (user) {

            case "goblin":
                console.log("Hello, you are going to have a fun journey as a goblin");
                var meetAnt = prompt("You run into a giant ant eater, you can attack or run. Do you choose to attack?").toLowerCase();
                if (meetAnt == "yes") {
                    console.log("you stab him in the eye, but then he hits you with his trunk. You are injured");
                    var finishAnt = prompt("you can either risk your life and finish him, or try to run away. Do you finish him?").toLowerCase();
                    if (finishAnt == "yes") {
                        console.log("congratulations you have killed the aint eater!");
                    }
                    else if (finishAnt == "no") {
                        console.log("You run away and barley make it, you are badly hurt");
                    }
                    else {
                        console.log("sorry thats not an option");
                    }
                }
                else if (meetAnt == "no") {
                    console.log("You run past the ant eater and get to saftey. You are very lucky")
                }
                else {
                    console.log("Sorry that is not an option.")
                }
                break;


            case "knight":
                console.log("Hello, you are going to have a fun journey as a knight");
                var dayFight = prompt("You are versing the goblins in a fight, you need to choose how many days you go to fight(1-100). Remember if you choose too little days then you won't have stayed long enough to win the fight but if you stay to long you have more of a chance of dying or getting injured").toLowerCase()
                if (dayFight = parseInt(dayFight, 10)) {
                    console.log("You did not fight enough days, which has resulted in your kingdome loosing the war to the goblins.");
                }
                else if (dayFight > 60) {
                    console.log("You have went to war to long and been seriously injured.");
                }
                else if (40 <= dayFight && dayFight <= 60) {
                    console.log("You have been at war for the right amount of time. You have came out of the war with no injuries and you also have defieted the goblins!");
                }
                break;


            case "troll":
                console.log("Hello, you are going to have a fun journey as a troll");
                var red = true;
                var green = true;
                var yellow = false;
                var blue = false;
                var houseRaid = prompt("You see four huts in the middle of the woods. You can raid two. There is a red hut, blue hut, yellow hut, and green hut. What is one color house that you want to raid?").toLowerCase()
                var doorPick = prompt("Do you want to enter throuhg the back door or front door?").toLowerCase()
                if ((doorPick || houseRaid == "red") || ("green" && "back door")) {
                    console.log("You raided these houses and left before any villagers would see you");
                }
                else {
                    console.log("You raided those houses, but one of them was a booby trap and you are now captured");
                }
                break;


            case "human":
                console.log("Hello, you are going to have a fun journey as a human");
                var reinforceFound = prompt("You know a storm is comming and you have to reinforce your hut, but you only have enough material to reinforce either your lower foundations or higher foundations. Which do you inforce? Higher or lower?").toLowerCase()
                if (reinforceFound == "lower") {
                    console.log("The storms winds pushed down the top of your house and caved in your roof.");
                }
                else if (reinforceFound == "higher") {
                    console.log("The storm did not do that much damage to your house due to your reinforced higher foundations. Good choice");
                }
                else {
                    console.log("sorry but that is not an option. Pick either 'higher', or 'lower'")
                }
                break;


            case "wizard":
                console.log("Hello, you are going to have a fun journey as a wizard");
                var blood = true;
                var dust = true;
                var wings = false;
                var mushrooms = false;
                var postion = prompt("You are working on a new healing potion but you do not know what you need to add to finish it. You have 4 ingrediants; blood, dust, wings, mushrooms. Pick one too add. (WARNING: Pick carefully because if you choose the wrong ingerdiant, then your potion will be ruined.)").toLowerCase()
                if (postion == "wings" || postion == "mushroom") {
                    console.log("You picked a bad ingrediant and now your potion is ruined.");
                }
                else if (postion = "dust" || "blood") {
                    console.log("you picked the right ingrediant and your potion is okay")
                }
                else {
                    console.log("sorry but that is not an option");
                }
                break;

            default:
                console.log("Sorry but that is not a character in the game");
        };
    }
 
Share this answer
 
v2

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