Problems for understanding Selecting from Many Options with Switch Statements

Tell us what’s happening:
Dear community,
First Happy Sunday to everyone :slightly_smiling_face:
i wish you all a lovely weekend.
I am practicing Js and i got to solve the exercise of selecting from many options with Switch Statements, but i have some questions in regard of it.
In the part of Case values, the text says case values are tested with strict equality (===).
If it is so, why in the solution part we can´t use strict equality symbol? because it only works with
the symbol of assignment operator (=). Or perhaps when it says sets answer , it refers that we need to use the assignment operator ?.
Thank you all for the orientation and advise in this topic,
Kind regards,
Ivonne

Your code so far


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


// 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/79.0.3945.117 Safari/537.36.

Challenge: Selecting from Many Options with Switch Statements

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

the case and the value of the variable are compared using strict equality

so

switch (variable) {
  case possibleValueOne:
  ...
  case possibleValueTwo:
  ...
}

it’s like writing

if (variable === possibleValueOne) {
  ...
} else if (variable === possibleValueTwo) {
  ...
}

1 Like

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.

The cases already serve as equality. Each case checks that.

case 1: // as in val === 1 if false, check the next case

And once it finds a case that matches, it falls through, which is the essence of the

break;

1 Like

Dear @ilenia,
Thank you so much for the easy understandable explanation , i appreciate it :pray:
have a blessed day, and thank you again
Kind regards
Ivonne

Dear @ibnlanre,
Thank you so much for the kind reply and the easy understandable explanation
about the usage of switch and break.
I appreciate it :pray:, thank you again and i wish you a blessed day,
kind regards,
Ivonne

1 Like

Dear @ilenia,
I will do it thank you again.
kind regards,
Ivonne