Switch not working, please help :)


#1


Okay, for some reason my code isn't working and I don't know why, I don't get any errors but it's not working as intended.


As you can see it should say once you have put in the name either Chloe, Paula, Gary or Jack you have selected: then the name, but for some reason it just goes straight to default, can someone please help?


var user = prompt("What is your name on this adventure?","Jack, Gary, Chloe or Paula?").toUpperCase();

switch (user)
{
    case 'Jack':
        console.log("You have selected: Jack");
        break;
        
    case 'Gary':
        console.log("You have selected: Gary");
        break;
        
    case 'Chloe':
        console.log("You have selected: Chloe");
        break;
        
    case 'Paula':
        console.log("You have selected: Paula");
        break;
        
        default:
        console.log("Sorry, but that isn't a name.")
}


#2

HI just remove the .toUpperCase() to the prompt here

var user = prompt("What is your name on this adventure?","Jack, Gary, Chloe or Paula?").toUpperCase();

and (after remove the .toUpperCase()) remember to type in the console Jack, Gary, Chloe or Paula with the first letter uppercase.

OR

you can uppercase your case

    case 'JACK':
    case 'GARY':
    ...

#3

Thank you so much! This helped loads


#4

@giganinja68020,
The problem is you using the toUpperCase() Method
on the variable user which has an associated string Value

In your case Clause you are then using a Capitalized string Value

You will end up with the default case as outcome.


#5

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