freeCodeCamp Challenge Guide: Selecting from Many Options with Switch Statements

Okay, al lot has been said about this hint, I’d like to contribute how i experienced this:
I was able to solve it without testing with strict equality, so case values are not necessarily tested with strict equality then?

Here my solution with extra commentary:

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

switch (val) {
case 1: // just number the cases to make them different from eachother (instead of doing case value1), or else you get an undeclared variable
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);

2 Likes

How to delete this one?

Thank you teashton!!!

I would love to know how you figured this one out. What I came up with so far from correct it was ridiculous…

1 Like

Thanks so much, it was really helpful

I did not understand this one bit until I read the hints.
I had to read the hints for the golf code too.
Am a stupid and can’t understand javascript?

4 Likes

No, syper85. I think JavaScript is hard to learn online. I keep bouncing around to different sites reading and doing the lessons trying to wrap my head around it. Reading the MDN can be really boring and make my mind feel like it’s going to explode sometimes!

This was a poorly written lesson and exercise, imo. Up to this point, many of the exercises seemed too easy like the 4 or 5 where we were just adding < or > etc. But this one didn’t really explain the new concept or the syntax very well at all. It makes sense that the golf exercise was supposed to be a bit challenging since it was summing up several lessons.

I think that zhhyman, Razorsharpy and JoccaWeb are correct because in the instructions they are asking us to set the “answer” which, I believe, means that they are asking us to change the value of the variable “answer” for each case.

How I understand it is “case” is a JavaScript word. The word or number following it is the argument that must be matched absolutely. Then the statement below is what is executed if that argument matches, just like an if loops.

6 Likes

Even though I have done switch statements several time before, this one got me at me at one of my dumbest moments and made me take a look at the hints. A bunch of people have already done their bit in giving the solution but I noticed nobody took care of the default statement. I am therefore providing my solution too.

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=“There is no result for “+val+”.”;
break;
}

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

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

7 Likes