freeCodeCamp Challenge Guide: Selecting from many options with Switch Statements

freeCodeCamp Challenge Guide: Selecting from many options with Switch Statements
0

#1

If you have many options to choose from, use a switch statement. A switch statement tests a value and can have many case statements which define various possible values. Statements are executed from the first matched case value until a break is encountered.

Here is a pseudocode example:

switch (num) {
  case value1:
    statement1;
    break;
  case value2:
    statement2;
    break;
...
  case valueN:
    statementN;
    break;
}

case values are tested with strict equality (===). The break tells JavaScript to stop executing statements. If the break is omitted, the next statement will be executed.

(num) is the argument which its value will be compared with the Values ( value1, value2, … valueN) to pick a block of code to excute. The statement is the action will take place when its case value is matched with the (num) value.


#2

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


#3

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


#4

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.


#5

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


#6

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


#7

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


#8

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.


#9

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’


You shouldn’t have four breaks no break after the last one
#10

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.


#11

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


#13

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.


#14

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


#15

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.


#16

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


#17

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.


#18

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.


#19

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


#20

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


#21

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