Logical Operators: Unexpected Else Token & Format Question


#1


https://www.codecademy.com/en/courses/javascript-beginner-en-ZA2rb/0/5?curriculum_id=506324b3a7dffd00020bf661#


Everytime I try to run the code I get a syntaxError: Unexpected token else

I've fiddled around with it a lot and haven't been able to correct the issue, but my larger question is whether I've sequenced my code incorrectly. I wanted to try something a little different than the default template they gave you. I wanted you to have an identical set of choices that have different outcomes based on what you put in your first answer. In the service of this I included by if else statement after the initial case check - will this work or do I need to nest the if else statements into each original case statement?

Thanks!


var user = prompt("Are you a wizard, 'Arry?").toUpperCase();

switch (user) {
    case 'YES':
       var decision = prompt("Damn skippy. Not the brightest bulb though. You're currently in the forbidden forest on a dare and you're lost. Do you want to TRY TO FIND YOUR WAY OUT or STAY PUT?");
    break;
    case 'NO':
        console.log("Hahaha, of course not! My mistake! Magic isn't real! I didn't fly here on a motorcycle, see ya later Mr. Muggle")
    break;
    case 'HELL YEAH':
        var decision = prompt("Oh damn, you're not just any old Wizard. You're the Wizard King. You're wandering around the forbidden forest, but it's getting kind of boring. Do you want to TRY TO FIND YOUR WAY OUT or STAY PUT?").toUpperCase();
    break;
    default:
    console.log("Don't be cute, it's not nice to make computers look stupid");
   if (decision === 'TRY TO FIND YOUR WAY OUT' && user === 'YES') {
         console.log("You wander around trying to find your way out, but             you're useless with Hermoine. A werewolf finds and eats you.")
    } if else (decision === 'TRY TO FIND YOUR WAY OUT' && user === 'HELL YEAH') {
        console.log("You fly up into the night sky cackling like a mad man. Fuck this forest. You're gonna go play minigolf. You're the best at minigolf.")
    } if else (decision === 'STAY PUT' && user === 'YES' || 'Hell Yeah') {
        console.log("Pretty boring decision, but a smart one. Hermoine finds you, and as remarkable as you are, she's a hell of a lot better. You guys hang out and do cool forest stuff.")
    } else {
        console.log("Play the game by the rules, man.")
    }
    
}


#2

in JavaScript it is else if not if else :slight_smile: .


#3

By the first
Console.log you forgot the semicole to put back behind it, not sure this is the problem.


#4

Good eye! But it doesn't fix the problem :confused:


#5

I believe if else is used if there is more than two outcomes that you want to check, so while a standard binary readout would be:

if (condition) {
do this
} else {
do this
}

A scenario with three conditions would be written as follows
if (condition) {
do this
} if else (2nd condition) {
do this other thing
} else {
Do the third thing
}


#6

it is else if , not if else .


#7

Oh man, you're a genius. My code isn't logging the endings based on the else if statements, but at least it's running now. Thanks!


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.