5. Logical operators error


I'm getting a syntax error for an unexpected token { somewhere in my code, I've pasted it into word to ctrl+F and I have the same amount of '{' as I do '}', so i'm assuming one is out of place? I'm not sure, here's my code:

var user = prompt("You're strolling through the woods when you find a troll guarding a bridge. Do you FIGHT him, PAY him, or RUN?").toUpperCase();
switch(user) {
    case "FIGHT":
        prompt("You can take this troll. Are you STRONG, SMART or BOTH?").toUpperCase();
        If("STRONG" === 1){
            console.log("Your impressive strength allowed you to overpower the troll, but only just. You've sustained serious injuries.");
        else if("SMART" === 1){
            console.log("Your cunning wits allow you to dodge the trolls heavy attacks and he slips into the river, apparently unable to swim. You cross the bridge safely");
        var "BOTH" = BOTH1
        else if(BOTH1 === 1){
            console.log("You display an impressive combination of attack and skill, dispatching of the troll with ease. Your brovado soon gets the better of you, however, as you slip clumsily on the wet bridge and fall overboard.");
case "PAY":
    prompt("You decide to play it safe. Do you offer him TREASURE, your MONEY or EVERYTHING?").toUpperCase();
    if("TREASURE" === 1){
        console.log("You rummage through your bag, unable to find something of significant value, besides your only defence; an ancient sword.");
        prompt("Do you want to give up your sword? YES or NO").toUpperCase();
        if("YES" === 1 && "TREASURE" === 1){
            console.log("You begin to pull out your sword to offer it to the troll. As soon as he sees the glowing blade, he becomes threatened and swings his great club around to your head, knocking you unconcious.");
        else if("NO" === 1){
            console.log("Before you can reach for your coin purse, the easily irritable troll becomes disgruntled, tired of waiting. He decides the best course of action is to sweep your legs and take your treasure and money forcibly. You have no choice but to leave defenceless and penniless");
    else if("MONEY" === 1){
        console.log("You toss your coin bag to the troll which he stares at for an uncomfortable amount of time, it's apparent he's trying to count how much you've handed over.");
        console.log("Now enjoying the weight of the bag, he steps aside to let you pass. You nervously, but safely, cross the bridge.");
    var "BOTH" = BOTH2
    else if(BOTH2 === 1){
        console.log("There's no need to take any unnecesary risks here. Slowly and carefully, you bring out your only treasure, your sword, and at the same time, your coin purse. The troll accepts your generous offer without hesitation and lets you pass.");
        console.log("Defenceless, you continue through the woods until you come across a snake that springs forward and bites your leg. You've contracted dysentery and die shortly after.");
case "RUN":
    prompt("It's not a smart move to gamble with a troll. Do you decide to run across the BRIDGE or BACK from where you came?").toUpperCase();
    if("BRIDGE" === 1){
        prompt("Are you FAST, AGILE or BOTH?").toUpperCase();
        if("FAST" === 1){
            console.log("You burst into a sprint across the bridge as fast as you can! All it took for the troll to send you to a grinding hault, however, was his out-stuck foot. You launch forward on your stomach to be captured by the troll.");
        else if("AGILE" === 1){
            console.log("You sprint across the bridge, adrenaline fuelling your body, narrowly dodging the trolls swings and barrages. Once you've cleared his bridge, you see he's stopped chasing you, deciding instead to keep guard for his next victim.");
        var "BOTH" = BOTH3
        else if("BOTH3" === 1){
            console.log("You dodge, duck, dip, dive and dodge past the trolls relentless attacks, running all the while as fast as you are able. With little effort and nothing more than some bumps and scrapes, you make it across the bridge to safety.");
    else if("BACK" === 1){
        prompt("Are you FAST, AGILE, or BOTH?").toUpperCase();
    if("FAST" === 1){
        console.log("You quickly decide this whole situation isn't worth the risk. Spinning on your heel, you turn and run as fast as you can in the opposite direction, and away from the troll.");
    else if("AGILE" === 1){
        console.log("Becoming increasingly nervous as you approach the troll, you make a snap decision to dart the other way as fast as you can. Turning on the spot to be ready to sprint, you move slightly too quickly, causing the troll to feel threatened and charge. Still trying to recover from your fright, you were not able to outrun the troll.");
    var "BOTH" = BOTH4
    else if(BOTH4 === 1){
        console.log("Feeling confident in your ability to run away, you do so without issue. You begin to walk to the troll, glance at his bloody club and swiftly turn in the other direction.");
    console.log("The troll is confused by what you tried to do and becomes enraged. He catches you off-guard, kills you and shrugs your body into the river.");


Huh, what is BOTH1, ..., BOTH4? They are not strings nor variables. That is the problem. Set them to some strings and your code should work.


I'm fairly certain this had the same issue before I put those in. Those are so if the user says BOTH for question one, then question two won't assume BOTH is still true.

I'll try anyway and let you know.


Ok, but you can't write var "BOTH" = BOTH1, it means nothing, maybe var Both1 = "Both"; is what you wanted to do there? Also inside your if, else if(BOTH1 === 1) makes little sense and code like this can cause you trouble in the future. Try to compare variables with things you expect inside, and you can't expect 1 if you want to assign string 'Both' to it. Let me know if changing this variables helped you. If not, I can provide you with my code.