I almost had it i think


#1

Hello everyone i am confused. i keep getting this "else" error and i don't know why. i have evaluated my code and i don't see where i went wrong

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

syntax error: unexpected token else


var user = prompt('You awaken in a cave due to water dripping on your forehead. Its dark, damp and cold. As you sit up, you notice a dim flickering light. What will you do?').toLowerCase();
switch(user) {
    case 'follow the light':
    var toLight = prompt('you follow the light. As you follow the seemingly never brightening light, you trip on something. its a body. study the body?').toLowerCase(); 
    var study = prompt('you study the body. Its hard to see in the dim light, but from what you notice, it looks like the body is a man. will you check his pockets for anything useful?').toLowerCase();
    
    
    if (toLight === 'yes' && study === 'yes') {
        console.log ('you find a half full lighter. you can now see in the dark for a short time.');
        
        else {
            console.log ('nothing happens')
            }
        }
        break;
    
    case 'find your own path':
        var yourPath = prompt('you fumble around in the darkness for a while until you find a wall. you say to yourself "should i follow the wall to the left or to the right?" ').toLowerCase();
        
        var toLeft = prompt('you follow the wall to the left but slip and fall into a chasm. luckily there was a large rock shelf not too far from where you fell."I cannot see" you tell yourself. through your blindness you hear water comming from the right. go to it?');
        var toRight = prompt('you follow the wall to the right. As you follow the wall your hand rubs against something and a loud "click" echos through the cave. soon after the ground shakes, you fall to the ground and see a rock start to raise revealing a lit hidden pathway. follow the lit passage?');
        
        if (yourPath === 'left' || yourPath === 'right') {
            return true;
            
            else if (yourPath === 'left' && toLeft === 'yes') {
                console.log ('you follow the sounds of rushing water until you find a waterfall. unfortunately the minute you find the waterfall, you slip on something and fall into the hole where the waterfall was. as you fall into the pool below you hit your head on a large sharp rock and die instantly.');
                }
            
            else if (yourPath === 'left' && toLeft === 'no') {
                console.log ('you decide not to follow the sounds of rushing water. instead you try to climb back up. to your sucess, you climb back to where you once were only to be staring at a wall of pure darkness that stretches only deeper into the cave');
                }
            else if (yourPath === 'right' && toRight === 'yes') {
                console.log ('you follow the passage. the moment you walk in, the passage way door shuts trapping you in. there is only one way to go now, but at least you can see');
                }
            else if (yourPath === 'right' && toRight === 'no') {
                console.log ('you decide not to follow the lit path but instead keep your path down the darkness. you walk for what seems like an eternaty in the darkness.');
                }
            else {
                console.log ('nothing happens');
                }    
            }
        
        break;
    
    case 'stay put':
        var stay = prompt('you stay in the spot you awoke in. as you waited in the darkness, you hear footsteps coming close to you. what do you do?');
        if (stay === 'run') {
            console.log ('you get up from your spot in a rush and start booking it in a random direction. the footsteps are close behind you as you run for your life into darkness. you slip and fall hard on the rock floor hitting your head knocking yourself unconscience.');
            else {
                console.log ('you stayed put as the enclosing footsteps came to a stop directly behind you.');
                }
            }
        break;
    
    default: console.log ('you yell into darkness');
    break;
    
    
    }


#2

That error message tells you exactly where the problem lies. Check all of your else and else if statements. Make sure you have the curly braces where they belong, at the beginning and end of code to execute.

sample if/else if/else statement

if (condition 1){
code code code;
} else if (condition 2){
code code code;
} else {
code code code;
};


#3

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