QUESTION Basic JavaScript: Selecting from Many Options with Switch Statements

My code passed the challenge but when I watched the video for further explanation after, the person was using answer = “alpha”. I have two questions, the first one is why it let me pass with the “return” instead of “answer”. The second question is why would the lesson tell me that switch tests with === operator? When I tried doing answer === “alpha” it wouldn’t read the value. But the instructor just used = , which I assumed you couldn’t use anyway because the lesson says otherwise. I was like damnn lol, if I had just used a single = it would have passed

Your code so far


function caseInSwitch(val) {
var answer = "";
// Only change code below this line
switch(val) {
case 1:
return "alpha";
break;

case 2:
return "beta";
break;

case 3:
return "gamma";
break;

case 4:
return "delta";
break;
}


// Only change code above this line
return answer;
}

caseInSwitch(4);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36.

Challenge: Selecting from Many Options with Switch Statements

Link to the challenge:

1 Like

Two things here:

why it let me pass with the “return” instead of “answer”.

Both approaches work just fine. He is simply storing it in a variable and then returning the variable at the end of the function. For this application, I like your approach better - just returning the value immediately in each case. In this case, you also don’t need the break statements because as soon as the function hits a return, it exits the function completely.

The second question is why would the lesson tell me that switch tests with === operator?

It is not saying that you should use a ===. It is saying that when it compares val to each case value, it does it with strict equality. In other words, if your first case was:

case "1":
return "alpha";
break;

it wouldn’t work because it would be looking for the string "1" and val is the number 1. Those are not strictly equal (===) they are only abstractly equal (==). In other words:

1 == "1"  // true
1 === "1" // false

It’s just reminding you that the types in you switch statement need to match the type for the cases.

You solution is a perfectly valid solution and is what I would have written - although I would have left of the redundant break statements - they are unreachable code. That being said, it looks like the test checks for them, so you need them here, but in the real world I would have left them off.

It’s probably requiring the break statements because they are assuming that you will solve it the same way as the guy in the video. But both solutions are equally valid, imho.

@anonbubble, your code has no connection with var answer="" and return answer; your code is directly running from switch with taking value from val of the main function.

You can check it by deleting var answer and return answer, then test your code, also use console.log() test what I mentioned. By deleting this, your code will also pass the test because it’s getting the expected value.

Now, come to your question: I didn’t see the video you are talking about, but that was updating the var answer and finally return the output via var answer. And getting the same result. Hope this will help you to understand.

Thank you.

1 Like

Yeah, that “answer” code was messing with me lol. I thought I had to do something in accordance to the “answer” variable or something. Thanks for clearing that up!

1 Like

I keep forgetting that “return” will return the value as well, so far I was just using it as a vessel to print the answer without knowing it’s job with the actual function. But the longer way perhaps is to store the variables and then invoke them to the console. If return does the same thing then I guess I made it easier for myself without even knowing lol. And I keep thinking you have to do stuff manually and forget that the “switch” statement does it’s own job of comparing variable types. I had the assumption I had to remind the switch statement to use === for it to compare properly. But I guess it was the simplicity of these functions is where I got confused haha. I appreciate the response!

Cool. And I’m not saying that storing the information in variable like he did in the video is a bad idea - in some cases I might do the same.

1 Like