freeCodeCamp Challenge Guide: Selecting from Many Options with Switch Statements

freeCodeCamp Challenge Guide: Selecting from Many Options with Switch Statements
0

Selecting from Many Options with Switch Statements


Problem Explanation

A switch statement first evaluates its expression. It then looks for the first case clause whose expression evaluates to the same value as the result of the input expression (using the strict comparison, (===) and transfers control to that clause, executing the associated statements. (If multiple cases match the provided value, the first case that matches is selected, even if the cases are not equal to each other.)

If no matching case clause is found, the program looks for the optional default clause, and if found, transfers control to that clause, executing the associated statements. If no default clause is found, the program continues execution at the statement following the end of switch. By convention, the default clause is the last clause, but it does not need to be so.

The optional break statement associated with each case label ensures that the program breaks out of switch once the matched statement is executed and continues execution at the statement following switch. If break is omitted, the program continues execution at the next statement in the switch statement.1


Hints

Hint 1

Remember that case values are tested with strict equality (===).

Try to solve the problem now!

Hint 2

Do not see “following conditions” as an ordered list as it looks in the original freeCodeCamp demo, but as values and statements, as shown here

Try to solve the problem now!


Solutions

Solution 1 (Click to Show/Hide)
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;
}

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

Code Explanation

It is common to ignore that case values are tested with strict equality with any need of other expression, like so:
case === value

Solution 2 (Click to Show/Hide)
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;
  }
  // Only change code above this line
  return answer;
}
// Change this value to test
caseInSwitch(1);

Code Explanation

Since you already have a variable defined at the beginning of the function named answer and it’s defined as the last return statement, you can assign new values to it for each case and will return the expected answer depending on the value you pass to the function.

Relevant Links

1. Description of “switch” - MDN JavaScript reference.

5 Likes

well, this wiki was no help at all. Just copypasta from the challenge.

23 Likes

Yes, not the most understandable lesson. I continue to think how to solve it

13 Likes

I found this challenge not very well explained. I worked it out after a few minutes but I still don’t feel like I have a solid grasp of switch statements.

16 Likes

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;
}

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

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

31 Likes

I tried so. Does not work. Returns an empty response …

Oh my God. It worked! Now we need to understand what the mistake was …
I think this is because of errors with the indication of variables

Thanks. The explanation on this one made no sense. The example says pseudocode, but it’s actually formatted like it would be in javascript. This one needs a rewrite.

7 Likes

Hello,
I am agree, this section lacks of explanations. Here is my code :

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;
}

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

It passes the test but I still have the following error :

Unreachable ‘break’ after ‘return’

5 Likes

Hey,

I was having the same issue with this. Eventually I found that the “unreachable ‘break’ after ‘return’” error went away once “return” was replaced with "answer=‘x’ ". I see @camperbot used ‘answer’ with success, as well.

I’m a beginner, so I am not sure, don’t quote me. However, I am assuming that the use of ‘var answer="";’ at the beginning and ‘return answer;’ at the very end (in the untouchable portions of this switch statement) is the key to knowing how to format the ‘statement’ you will generate with a ‘true’ case.

It is not explained well and the pseudocode didn’t actually help me.

Also, if this isn’t the accurate reasoning for using answer vs return, please let me know.
I would love to know for sure, one way or another, so I don’t confuse myself anymore. :grinning:

Update: on the next task I noticed that they do mention ‘answer’ in the instructions.

“Instructions
Write a switch statement to set ‘answer’ for the following conditions:”

Again in the untouchable parts of code they again use ‘answer’ in the same way, ‘return’ gave an error and ‘answer’ did not.

Update 2: A few tasks later and I found this tidbit of information.

When a return statement is reached, the execution of the current function stops and control returns to the calling location.

12 Likes

Thanks for this! Couldn’t wrap my head around it.

1 Like

Yeah, this one was not very well explained. I used the link to work it out. switch and case introduced new syntax without any explanation which threw me off. I finally worked it out. You call in switch and then enter in the variable you want tested within the (). Your cases are essentially little if statements, for example:

(call it in) switch (variable) {
(if) case: “whatever” <-- case is the variable you called above.
(then) whatever you want to happen
break;
}
The break will end the switch and jump to the end of it.
I hope that makes sense.

7 Likes

strong text

SPOILERS!!!

Here’s the solution i had a heck of a time figuring it out… It was not explained very well. hope this helps!!!

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;}

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

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

5 Likes

teashton, when you say it worked how do you know it worked? did the message drop-down with an encouraging text, asking you to go to the next challenge?

Because I tried this, and…nothing. just the ‘//testing challenge’ message, then a black bar.

So then I wrote this:
switch (val){

case 1:
  console.log("alpha");

break;

case 2:
console.log(“beta”);
break;

case 3:
console.log(“gamma”);
break;

case 4:
console.log(“delta”);
break;
}

Hoping that it will print(console.log). Nothing. Interestingly does not give an error message either. Just blanks out.

2 Likes

I tried teashton’s method and it worked, check your syntax. This challenge was particularly frustrating…

Thank you. I tried again, and it did work. Don’t understand why it didn’t work the first time round. Still wonder why the console.log method did not work.

Been a couple of hours sitting on this challenge. The code works on my Chrome console as you can see on my right side. Looks like the system is really expecting something…

using console.log()

using return:

Still no good using these approaches :joy:

ANOTHER EDIT

Instead, I followed @zhhyman’s example above. Just be keen on the syntax.

3 Likes

Thanks Julian, I think you nailed it.
And makes total sense.

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;
default:
answer = ‘default’;
}

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

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

2 Likes

function caseInSwitch(val) {
var answer = “”;
// Only change code below this line

switch (val) {
case 1:
if(val === 1){ answer = “alpha”; return answer; }

break;
case 2:
if(val === 2){ answer = “beta”; return answer; }
break;
case 3:
if(val === 3){ answer = “gamma”; return answer; }
break;
case 4:
if(val === 4){ answer = “delta”; return answer; }
break;
}

// Only change code above this line

}

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

2 Likes