4/6 Switch. Only default happening


#1



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


For some strange reason when I run this code it for some strange reason only runs the default even though the name is over 5 letters long. Could someone please help me with why this is happening.


var user = prompt("What is your name, player").toUpperCase();
switch(user) {
    case user.length < 5 && user.length > 2:
        console.log("You have a short name " + user);
        break;
    case user.length > 4 && user.length < 7:
        console.log("That name is about average length " + user);
        break;
    case user.length > 6:
        console.log("Wow, you have a long name!");
        break;
    case user.length === 0:
        console.log("That's not even a name, you didn't type anything");
        break;
    default:
        console.log("Is that even a name?");
}


#2

Please check switch syntax. In switch's cases you don't check if something is true or false, you are checking if some variable value is equal to case value.

var animal = prompt("Choose animal: dog, cat");
switch(animal) {
    case "dog":
        console.log("bark bark");
        break;
    case "cat":
        console.log("meow");
        break;
    default:
        console.log("invalid animal");
}

Continued on
#3

So would i have to do it like this?

var user = prompt("What is your name player").toUpperCase();
switch(user) {
case (3 || 4)
console.log("You have a short name")
break;


#4

No, you should have cases like:

var user = prompt("What is your name player").toUpperCase();
switch (user) {
    case "WILL":
        console.log("Hi Will, you have a nice name.")
        break;
    case "ADAM":
        console.log("Hi Adam, you have such old name");
        break;
    default:
        console.log("Hi " + user);
}

#5

Oh man, I feel like such an idiot. You are a life saver man


#6

@willofiron if you want to check user.lenght you can use switch like that:

var user = prompt("What is your name player").toUpperCase();
switch (user.length) {
    case 0:
        console.log("That's not even a name, you didn't type anything");
        break;
    case 1:
    case 2:
        console.log("Is that even a name?");
        break;
    case 3:
    case 4:
        console.log("You have a short name " + user);
        break;
    case 5:
    case 6:
        console.log("That name is about average length " + user);
        break;
    default:
        console.log("Wow, you have a long name!");
}

#7

Thanks that is actually really useful


#8

Hope you understand it now :slight_smile:


#9

I do now and thanks for all your help


#10

I'm having the same issue but my code in the cases seems to be formatted properly. All I ever receive is the default reply

var user = prompt("What's going on?").toUpperCase(); 

switch (user) { 
    case "nothing":
        console.log("Nothing is happening lad");
        break; 
    case "everything": 
        console.log("Everything is happening lad");
        break; 
    case "celebrating": 
        if (day == 1 && month == 1) { 
            console.log("Happy New year") 
        } 
        else if (day == 7 || month == 31) {
            console.log("Happy End of the Week/Month") } 

            else {
            console.log ("Not a holiday but a lovely day!") } 
            break; 
    case "something": 
        console.log("All I know is that something is happening"); 
        break; 
    default: 
        console.log("Please put in a response" ); 
}

#11

You are updating user's input to be upper case...

and then checking cases in lower case. That's why you got always default option.

Change your cases to upper case ("NOTHING" etc.) or change user input toLowerCase().


#12

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