Switch using .length to determine case


#1



I want to use the length of user to determine what case to do. My eventual goal is to have a couple prompts that take your name and age and a few other factors and store new variables based on the length or number of each answer, and then call those variables at the end to console.log a poem.

I.E., user.length <4, define var short to = some part of the poem.
age >40, define var middleAge to = a different part of the poem
confirm the information gathered, and console.log short, middleAge, etc.

I've read that the better way to do this is if/else statements, not switch, but is there a way to use switch with a .length?

(code below is just a small piece to try and work out the bugs)


var user = prompt("What is your name?").length;

switch(user) {
    case (5):
       var short = "i am short and sweet\
        like a butterfly's life\
        enriching the world\
        in such short time";
        break;
        
     default:
        var noChoice = "i am locked potential\
        the vast ocean of infinite choice\
        once released ever effecting, growing, changing";


#2

@jmkaden,

try ...

switch(user.length) {

... etc. Does it work?


#3

I tried that and it doesn't work


#4

@jmkaden ,

What have you done to test whether or not it works? Note that the code you posted does not contain any calls to console.log or any other functions that produce output, so you may not see the result.

Also note that you should not be using .length here ...

var user = prompt("What is your name?").length;

It is better to use it in the switch header.


#5

I know it's not working because it's returning an error: SyntaxError: Invalid or unexpected token


#6

I tested your idea with this, which includes calls to console.log, and it worked ...

var user = prompt("What is your name?");

switch(user.length) {
    case (5):
       console.log("i am short and sweet\
        like a butterfly's life\
        enriching the world\
        in such short time");
        break;
        
     default:
        console.log("i am locked potential\
        the vast ocean of infinite choice\
        once released ever effecting, growing, changing");
}

Feel free to adapt it to your needs. You will need to enhance it in order to satisfy Codecademy.


#7

Hm.. ok. Maybe the switch syntax needs to have something print to the console to work.


#8

Also: I was missing a closed curly bracket at the end


#9

No, you are not required to display output within a switch structure. You can instead assign values to variables within the switch structure if you want, then use those variables outside the switch structure later in your program. However, make sure that you do not try to use the value of a variable before it has been assigned a value. You had two assignments, each to a different variable, within your switch structure, but only one of them will get executed.


#10

Right, I understand. Interesting. Maybe my only issue was the curly bracket originally then. But thank you for your help, your feedback helped me make a better version of the code actually!


#11

Actually, one more question. Could I change it to be case (<5)? When I try it, it says the "<" is an unexpected token

var user = prompt("What is your name?");

switch(user.length) {
case (< 5):
console.log("i am short and sweet\
like a butterfly's life\
enriching the world\
in such short time");
break;

 default:
    console.log("i am locked potential\
    the vast ocean of infinite choice\
    once released ever effecting, growing, changing");

}


#12

No, <5 is not a complete expression. You can use user.length < 5 as your expression, but since that has only two possible outcomes, namely true or false, you might as well use an if-else structure instead.


#13

Ok, I see. Thanks for the help!


#14

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