"Selecting from Many Options with Switch Statements "

I don’t know what I am doing wrong. If anybody can point out the mistakes, I would appreciate it.


function caseInSwitch(val) {
  var answer = "val";
  // Only change code below this line
  switch(val) {
    case "1":
      "alpha";
      break;
    case "2":
      "beta";
      break;
    case "3":
      "gamma";
      break;
    case "4":
      "delta";
      break;
  }
  // Only change code above this line  
  return answer;  
}

// Change this value to test
caseInSwitch(1);

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/selecting-from-many-options-with-switch-statements/

      "alpha";

The lines like this are not JavaScript commands. They don’t do anything. You are always returning “val” because that is what answer is set to and you return answer.

Also, numbers are different from strings so 1 is not the same as "1".

So should I write just 1 instead of “1”?
Also, instead of writing “alpha”, what would the correct thing to write be?

Never mind I understand now. Thank You!

@ArielLeslie regarding this challenge, why does it state that one needs to use strict equality when using case values.

I was able to resolve the challenge but that part there doesn’t make sense since, in the first place, I didn’t need to use the strict operator.

Ex:
case 1: answer = "value";

BTW were you able to place an empty string to pass the challenge before initializing the switch statement?

Ex:
var answer = " ";

case 1: answer = "value"; This isn’t an equality check at all. It’s an assignment.

Right, that’s what I mean but on the challenge it mentions to use strict equality === when in fact it doesn’t need to be used.

If you look right below the example of pseudocode, it mentions “case values are tested with strict equality (===)”.

Why?

Because

switch(val) {
   case "bob":
   // do something
}

It’s like writing

if (val === "bob") {
   // do something
}

It’s the switch statement that tests the cases with strict equality

switch(val){
case 1: }
which is equal to if(val ===1){`

}

1 Like

So in a way it’s using the operator is in stealth mode…:thinking:

It is a way to have less code written, a switch statement easily substitute a long line of if…else if statements when you need to do a different thing with different values of a variable

It’s my understanding that with switch statement reduces use of system resources. Compared with nested if else statement.

Switch doesn’t substitute nested if else statements, but chained if else statements
I don’t know about perfomance tho

switch(val) {
   case "bob":
      // do something
   case "rob":
      // do something
   default:
      // do something else
}

the above is the same as the following chain:

if (val === "bob") {
   // do something
} else if (val === "rob") {
   // do something
} else {
   // do something else
}

the following instead are nested if statements:

if (...) {
   if (...) {
      // bla
   }
}
2 Likes

Thank you for valuable information.