SyntaxError: Unexpected token '{'. Parse Error


#1

So I'm working on a text based adventure of encountering a witch for this activity, which is Switch, no. 4 of Code Your Own Adventure 2! When I tried executing the code, it says SyntaxError: Unexpected token '{' Parse Error. Can someone help me, because I'm still not 100% used to this yet. Thanks :smile: And yes, I know the weapon isn't really necessary, but I wanted to add it anyway. Knowing me, the error's probably really obvious to you guys, so don't yell at me for not noticing a simple one, ok? I'm learning.

My code:

var user = prompt ("One day, while walking through the forest, you encounter a strange old woman, who turns out to be a WITCH! Do you TRICK her, RUN away or try to FIGHT?!").toUpperCase();

switch (user) {
    case 'TRICK':
        var smart = prompt ("Are you smart? YES, or NO").toUpperCase();
        var witty = prompt ("How good are you at thinking on the spot under pressure? Pick a number from 1-5, with one being horrible, and 5 being amazing at it");
        if (witty >= 3) {
            var witty = true;
        }
        if (smart === YES) {
            var smart = true;
        }
        else {
            var smart = false;
        }
        if (smart && witty) {
            console.log ("You managed to trick the witch, and escaped. Congratulations! How did you do it? When you saw the burns on the witch, and no other damage on her, you realised that she was immune to everything exept fire. Knowing that, you kept her talking, distracting her from the fact that you took a match out of your pocket, lit it and fled for your life (Good thinking, carrying matches).");
        }
        else {
            console.log ("I guess you're not both smart, or able to think when put on the spot. However, you need both these traits to survive, especially when you chose to trick the witch. Sorry, but the witch has caught you, and forced you to drink a slow acting poison, while roasting you in a fire. *mutters* Witches these days, so focused on torture. Anyway, you're dead. Maybe you should have picked another option.");
        }
    break;
    case 'RUN':
        var fast = prompt ("You are pretty fast (witches aren't that quick, but are pretty good at running for such an old lady). So, was that statement true, or false?").toLowerCase;
        var time = prompt ("Is it in the morning, afternoon, or night?").toLowerCase;
        if (time === morning) {
            console.log ("Well, since it's in the morning, you have high visibility, and probably most of your energy, allowing you to run pretty fast.");
            var time = true;
        }
        else if (time === afternoon) {
            console.log ("In the afternoon? With the hot sun beating down on you, sapping your energy? Sorry, but with those odds, you'll probably lose the race to the witch");
            var time = false;
        }
        else (time === night) {
            console.log ("Well, I guess it's cool in the night, with a breeze, but there's a lot less visibilty. I'll leave this one up to chance.");
            var time = Math.round(Math.random());
        }
        if (fast&&time) {
            console.log ("You were both fast, with good chances, and due to that, managed to barely outrun the witch, and survived! The witch can't exit the borders of the forest, but you'll probably never even go near it again.");
        }
        else {
            console.log ("Sorry, but you needed to be fast, and have good odds to outrun the witch, who drank a speed potion, increasing her, well, speed. (duh) After that long chase, she was so annoyed at you, she decided to just boil you in a pot, then eat you. I guess you regret walking in the forest now.");
        }
    break;
    case 'FIGHT':
        var strategy = prompt ("You normally just charge into a fight, and try to win through brute strength. True, or false?").toLowerCase;
        var strength = prompt ("How strong are you? Pick a number from 1-5, with one being the weakest, and 5 being the strongest.");
        var weapon = prompt ("Did you bring a POCKETKNIFE with you, or DIDN'T BOTHER just for a stroll? (Don't lie. WHat would you actually do normally in real life.)").toUpperCase;
        if (strength >=4) {
            var strength = true;
        }
        if (weapon === POCKETKNIFE) {
            console.log ("You actually would? WHY? Anyway, with that weapon, I'll give you an extra chance. You have 50/50 chance of actually using it now.");
            var weapon = Math.random(Math.floor());
        }
        else {
            console.log ("At least you're honest. Bonus points to you! You're a good person, and the world needs more of you. However, a pocketknife could be utilized as a distraction (Won't be a good offensive weapon). Let's see how you do.");
            var weapon = false;
        }
        if (weapon === true) {
            console.log ("Acheivement unlocked: Honesty is the best policy!");
        if  (weapon && strength && !strategy) {
            console.log ("Congratulations! You managed to defeat the witch, using a combination of your strength, and strategy. The pocketknife wasn't really used in this type of fight. Finding some duct tape lying on the forest floor (Maybe the witch was doing some construction work. You never know...), you used it to shut the witch up, so that she couldn't utter any spells, and with your strength, killed her. She won't be bothering anyone again.");
        }
        else if (strength && !strategy){
           console.log ("Congratulations! You managed to defeat the witch, using a combination of your strength, and strategy. The pocketknife wasn't really used in this type of fight. Finding some duct tape lying on the forest floor (Maybe the witch was doing some construction work. You never know...), you used it to shut the witch up, so that she couldn't utter any spells, and with your strength, killed her. She won't be bothering anyone again.");
        }
        else {
            console.log ("You just didn't have the abilities to kill the evil witch. With some strategy, you probably would have dodged her attacks. You also need sufficient strength for the finishing blow. Turns out the pocketknife wasn't needed at all. The witch instead cast a sphere of water to enter your mouth while you were yelling 'CHARGE!!!' (perhaps not the best strategy), and then used it to fill your whole body and flood it, clogging your mouth, nose and ears. From it, you drowned, or choked to death. Sorry, but GAME OVER.");
        }
    break;
    default:
    console.log ("You don't get to pick that. While panicking, or in sheer terror, you instinctively have a flight or fight response, while only the most levelheaded ones can think, and attempt to outwit the witch. So, replay this game, and don't enter something that wasn't specified. Thanks.");
}

#2

@netsolver66049,
You are writing a =book= instead of code...

You will really have to read on about truthy and falsy in Javascript
https://developer.mozilla.org/en-US/docs/Glossary/Truthy

var a = "3";
if (a)
console.log("truthy");
else
console.log("falsy")

var user = prompt (
    "One day, while walking through the forest, "+
    "you encounter a strange old woman, who turns out to be a WITCH! "+
    "Do you TRICK her, RUN away or try to FIGHT?!").toUpperCase();

switch (user) {
    case 'TRICK':
        var smart = prompt ("Are you smart? YES, or NO").toUpperCase();
        var witty = prompt (
          "How good are you at thinking on the spot under pressure? "+
          "Pick a number from 1-5, with one being horrible, "+
          "and 5 being amazing at it");
        if (witty >= 3) {
            var witty = true;
            //1 and 2 are also =truthy=
        }
        if (smart === YES) {
            var smart = true;
        }
        else {
            var smart = false;
        }
        if (smart && witty) {
            console.log (
                "You managed to trick the witch, and escaped. "+
                "Congratulations! How did you do it? "+
                "When you saw the burns on the witch, "+
                "and no other damage on her, "+
                "you realised that she was immune "+
                "to everything exept fire. "+
                "Knowing that, you kept her talking, "+
                "distracting her from the fact "+
                "that you took a match out of your pocket, "+
                "lit it and fled for your life "+
                "(Good thinking, carrying matches).");
        }
        else {
            console.log (
                "I guess you're not both smart, "+
                "or able to think when put on the spot. "+
                "However, you need both these traits to survive, "+
                "especially when you chose to trick the witch. "+
                "Sorry, but the witch has caught you, "+
                "and forced you to drink a slow acting poison, "+
                "while roasting you in a fire. "+
                "*mutters* Witches these days, so focused on torture. "+
                "Anyway, you're dead. "+
                "Maybe you should have picked another option.");
        }
    break;
    case 'RUN':
        var fast = prompt (
            "You are pretty fast (witches aren't that quick, "+
            "but are pretty good at running for such an old lady). "+
            "So, was that statement true, or false?").toLowerCase;
        var time = prompt (
            "Is it in the morning, afternoon, or night?").toLowerCase;
        if (time === "morning") {
            console.log (
                "Well, since it's in the morning, "+
                "you have high visibility, "+
                "and probably most of your energy, "+
                "allowing you to run pretty fast.");
             
             //already defined var time = true;
             time= true;
             // all =time= value's are truthy !!!
        }
        else if (time === "afternoon") {
            console.log (
                "In the afternoon? "+
                "With the hot sun beating down on you, "+
                "sapping your energy? "+
                "Sorry, but with those odds, "+
                "you'll probably lose the race to the witch");
            time = false;
        }
        else if (time === "night") {
            console.log (
                "Well, I guess it's cool in the night, "+
                "with a breeze, but there's a lot less visibilty. "+
                "I'll leave this one up to chance.");
            time = Math.round(Math.random());
        }
        if (fast&&time) {
            console.log (
                "You were both fast, with good chances, "+
                "and due to that, managed to barely outrun the witch, "+
                "and survived! "+
                "The witch can't exit the borders of the forest, "+
                "but you'll probably never even go near it again.");
        }
        else {
            console.log ("Sorry, but you needed to be fast, "+
            "and have good odds to outrun the witch, "+
            "who drank a speed potion, increasing her, "+
            "well, speed. (duh) "+
            "After that long chase, she was so annoyed at you, "+
            "she decided to just boil you in a pot, then eat you. "+
            "I guess you regret walking in the forest now.");
        }
    break;
    case 'FIGHT':
        var strategy = prompt(
            "You normally just charge into a fight, "+
            "and try to win through brute strength. "+
            "True, or false?").toLowerCase;
        var strength = prompt(
            "How strong are you? Pick a number from 1-5, "+
            "with one being the weakest, and 5 being the strongest.");
        var weapon = prompt(
            "Did you bring a POCKETKNIFE with you, "+
            "or DIDN'T BOTHER just for a stroll? "+
            "(Don't lie. "+
            "WHat would you actually do "+
            "normally in real life.)").toUpperCase;
        if (strength >=4) {
            strength = true;
        }
        if (weapon === "POCKETKNIFE") {
            console.log (
                "You actually would? WHY? Anyway, with that weapon, "+
                "I'll give you an extra chance. "+
                "You have 50/50 chance of actually using it now.");
//whats'up??            var weapon = Math.random(Math.floor());
        }
        else {
            console.log (
              "At least you're honest. Bonus points to you! "+
              "You're a good person, and the world needs more of you. "+
              "However, a pocketknife could be utilized as a distraction"+
              " (Won't be a good offensive weapon). "+
              "Let's see how you do.");
            weapon = false;
        }
        if (weapon === true) {
            console.log (
                "Acheivement unlocked: Honesty is the best policy!");
        }
        if  (weapon && strength && !strategy) {
            console.log (
                "Congratulations! "+
                "You managed to defeat the witch, "+
                "using a combination of your strength, and strategy. "+
                "The pocketknife wasn't really used in this type of fight."+
                " Finding some duct tape lying on the forest floor "+
                "(Maybe the witch was doing some construction work. "+
                "You never know...), "+
                "you used it to shut the witch up, "+
                "so that she couldn't utter any spells, "+
                "and with your strength, killed her. "+
                "She won't be bothering anyone again.");
        }
        else if (strength && !strategy){
           console.log (
               "Congratulations! You managed to defeat the witch, "+
               "using a combination of your strength, and strategy. "+
               "The pocketknife wasn't really used in this type of fight. "+
               "Finding some duct tape lying on the forest floor "+
               "(Maybe the witch was doing some construction work. "+
               "You never know...), "+
               "you used it to shut the witch up, "+
               "so that she couldn't utter any spells, "+
               "and with your strength, killed her. "+
               "She won't be bothering anyone again.");
        }
        else {
            console.log (
                "You just didn't have the abilities "+
                "to kill the evil witch. "+
                "With some strategy, you probably "+
                "would have dodged her attacks. "+
                "You also need sufficient strength for the finishing blow."+
                " Turns out the pocketknife wasn't needed at all. "+
                "The witch instead cast a sphere of water "+
                "to enter your mouth while you were yelling 'CHARGE!!!'"+
                " (perhaps not the best strategy), "+
                "and then used it to fill your whole body and flood it, "+
                "clogging your mouth, nose and ears. "+
                "From it, you drowned, or choked to death. "+
                "Sorry, but GAME OVER.");
        }
    break;
    
    default:
    console.log (
        "You don't get to pick that. "+
        "While panicking, or in sheer terror, "+
        "you instinctively have a flight or fight response, "+
        "while only the most levelheaded ones can think, "+
        "and attempt to outwit the witch. "+
        "So, replay this game, and don't enter something "+
        "that wasn't specified. Thanks.");
}