Please Help! My Switch Case isn't working as expected


#1

I expect that when the user enters no, and is prompted to choose another option, the 1st switch statement ends and goes to the second switch statement where the console will output a statement depending on the user input.

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

SyntaxError: Unexpected token case

var user = prompt("What do you like the most?", "Money, love or power").toUpperCase(); //converts user input to upper case before assigning to user variable.
switch(user)
{
    case 'MONEY':
        userAnswer = prompt("Are you sure of your answer (yes, no)").toUpperCase();
        if (userAnswer === "YES")
        {
            console.log("Always remeber that money is the root of all evil");    
        }
        else{
            console.log( user = prompt("What do you like the most? Money, love or power").toUpperCase());
            }
            break;
    case 'Love':
            console.log("Alwyas remember that all in fair in love and war");
            break;
    case 'Power':
    console.log("Power can be the death of a villian");
    break;
    default:
    console.log("Please enter input from the listed option");
    break;
    }
    switch(user)
     case 'Love':
            console.log("Alwyas remember that all in fair in love and war");
            break;
    case 'Power':
    console.log("Power can be the death of a villian");
    break;
    default:
    console.log("Please enter input from the listed option");
    break;

#2

SyntaxError: Unexpected token case

missed a { and a } bracket for your second switch statement

and all characters of Love case and Power case should be uppercase

and why do you want to print same thing twice?


#3

The output should be the same. Because in the 1st switch statement, console.log will prompt the user to choose another option if the user enters "no". Hence, on choosing from the option, the program should go into the 2nd switch statement and produce an output for the user based on his choice.


#4

Here, all you need to do is say console.log(user) because you're user variable is already defined

to,

console.log(user);


#5

please understand me: if (user === "YES")
{
console.log("Always remeber that money is the root of all evil");
}
else{
console.log( user = prompt("What do you like the most? Money, love or power").toUpperCase());
}
break;
What this code means is that if the user doesn't enter 'Yes'. The user should be prompted to enter another option. When the user does that, I want the console to output a string to the user, depending on what he chooses.

That's what I am battling with.


#6

What do you want your string to say?


#7

Thanks for the help!! I was able to resolve it


#8

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