5. Logical Operators. I swear I don't know what's wrong


#1



Soooooo I keep getting the error of "Unexpected token case." I don't know what I've done wrong. Please help someone?? You'll receive eternal cosmic powers in another life! Maybe... I don't know.

var user = prompt("Which way do you go? LEFT, RIGHT, or FORWARD?").toUpperCase();
switch(user) {
    case 'LEFT':
    var choice1 = prompt("Are you brave?").toUpperCase();
    var choice2 = prompt("Are you smart?").toUpperCase();
    if(choice1 === 'YES' || choice2 === "YES") {
        console.log("You go left and fall down a cliff breaking your leg. Thanks to your courage and quick thinking, you jerry-rig a splint together and climb back up! You survive!");
    } else {
        console.log("You go left and fall down a cliff breaking your leg. Sadly, you aren't very bright and you simply starve to death while waiting for help. You lose!");
    break;
    case 'RIGHT':
        var money = prompt("Do you have any money on you?").toUpperCase();
        var dollars = prompt("Do you have any euros on you?").toUpperCase();
        if(money === 'YES' && dollars = 'YES') {    
        console.log("You go right and end up in Spain where a lovely young lady asks you if you'd like to go for a drink. Since you happen to have Euros on you, you not only buy this lovely lady a drink... but a meal and a ticket to the movies as well! Not only that, you both fall in love, get married and you work for her rich father on his large farm with the rest of the family. You win at life!");
    } else {
        console.log("You go right and end up in Spain where a lovely young lady asks you if you'd like to go for a drink. Sadly you have no money and you decline this beautiful woman's offer. You end up sad and lonely. You lose!")
    break;
    case 'FORWARD':
        var run = prompt("Are you going to run?").toUpperCase();
        if (run === 'YES') {
        console.log("You go forward at lightning speed, running so fast that you break the dimensional walls holding the universe together. You enter into another world wrought with conflict in epic space battle where Jedi and Sith battle for ultimate control of the universe. You suddenly find out you can use the Force. YOU WIN FOREVER!");
    } else {
        console.log("You go forward and simply keep walking into an endless void where life fades away into nothingness. Soon, you fade too. Returning to the nothing of which we all began. You lose?");
    }
    break;
    default:
        console.log("Hmm? What was that? I'm afraid I couldn't hear, what is your response?");
};


#2

Lool! I could understand that feeling. :smiley:

You really could have gone back to "What you'll be building" to cross-check! :smiley:

Well,see these:

In the above, I don't get, you aren't comparing rightly and try to keep double or single quotation rightly.

Keep on! See if that solves the issue(s).


#3

You didn't close the 'else' code block with '}' in any of this branches,
this should work

do this:

        } else {
            console.log("You go left and fall down a cliff breaking your leg. Sadly, you aren't very bright and you  simply starve to death while waiting for help. You lose!");
        }
        break;

instead of this:

    } else {
        console.log("You go left and fall down a cliff breaking your leg. Sadly, you aren't very bright and you  simply starve to death while waiting for help. You lose!");
        break;

Also, on this branch

        if(choice1 === 'YES' || choice2 === "YES") {
            console.log("You go left and fall down a cliff breaking your leg. Thanks to your courage and quick thinking, you jerry-rig a splint together and climb back up! You survive!");
        } else {
            console.log("You go left and fall down a cliff breaking your leg. Sadly, you aren't very bright and you simply starve to death while waiting for help. You lose!");
        }
        break;

Now with a close curly bracket hehe.

the if/then block executes if one of the choices are 'YES'
but doesn't care witch one, the else block only executes if none of the choices are set to 'yes'

then...

        case 'RIGHT':
            var money = prompt("Do you have any money on you?").toUpperCase();
            var dollars = prompt("Do you have any euros on you?").toUpperCase();
            if(money === 'YES' && dollars = 'YES') {    
            console.log("You go right and end up in Spain where a lovely young lady asks you if you'd like to go for a drink. Since you happen to have Euros on you, you not only buy this lovely lady a drink... but a meal and a ticket to the movies as well! Not only that, you both fall in love, get married and you work for her rich father on his large farm with the rest of the family. You win at life!");
        } else {
            console.log("You go right and end up in Spain where a lovely young lady asks you if you'd like to go for a drink. Sadly you have no money and you decline this beautiful woman's offer. You end up sad and lonely. You lose!");
        }
        break;

Now updated with a semicolon at the end of the 'else' console.log call and a closing brace at the end of the 'else' code block... don't hate :wink:

now the if/then block only runs if both choices are 'YES'
the else block is picked if

  • only one of the choices is 'YES'
  • none of the choices is 'YES'

Hope that helps. :smiley: