freeCodeCamp Challenge Guide: Multiple Identical Options in Switch Statements

freeCodeCamp Challenge Guide: Multiple Identical Options in Switch Statements
0.0 0

#1

If the break statement is omitted from a switch statement’s case, the following case statement(s) are executed until a break is encountered. If you have multiple inputs with the same output, you can represent them in a switch statement like this:

switch(val) {
  case 1:
  case 2:
  case 3:
    result = "1, 2, or 3";
    break;
  case 4:
    result = "4 alone";
}

Cases for 1, 2, and 3 will all produce the same result.


#2

function sequentialSizes(val) {
var answer = “”;
// Only change code below this line
switch (val) {
case “1”:
case “2”:
case “3”:
answer = “Low”;
break;
case “4”:
case “5”:
case “6”:
answer = “Mid”;
break;
case “7”:
case “8”:
case “9”:
answer = “High”;
}

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

// Change this value to test
sequentialSizes(2);

This is not the solution…but close try and find where I went wrong.


#4

I struggled with this one for a while. I knew I had it, but it would not give me the last check 'must have 9 cases.'
I noticed I had put a short comment in exactly where it is here. I removed it and it worked. I wonder how many times this has happened before and we never realized it?


#5

anyone know how to solve this


#6

function sequentialSizes(val) {
var answer = “”;
// Only change code below this line
switch(val) {
case 1:
case 2:
case 3:
answer = “Low”;
break;
case 4:
case 5:
case 6:
answer = “Mid”;
break;
case 7:
case 8:
case 9:
answer = “High”;
break;
}

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

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

i put my working code above but try it on your own. the thing i got caught up on was i didn’t add the switch(val) at the beginning i just started going straight into cases


#7

Just remove “” from, case… correct code is-
function sequentialSizes(val) {
var answer = “”;
// Only change code below this line
switch(val){
case 1:
case 2:
case 3:
answer = “Low”;
break;
case 4:
case 5:
case 6:
answer = “Mid”;
break;
case 7:
case 8:
case 9:
answer = “High”;
break;
}

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

// Change this value to test
console.log(sequentialSizes(1));


#8

Challenge is faced on that code of yours is the You should have nine case statements test case. But I figured out a way to passed it. Thanks guys for the contribution. Looks like this activity is not explained well but I learned a lot.


#9

remove the quotes in the cases


#10

There should not be quotes around the numbers.

function sequentialSizes(val) {
var answer = “”;
// Only change code below this line
switch (val) {
case 1:
case 2:
case 3:
answer = “Low”;
break;
case 4:
case 5:
case 6:
answer = “Mid”;
break;
case 7:
case 8:
case 9:
answer = “High”;
}
// Only change code above this line
return answer;
}

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


How can switch code formatting be improved to allow it to run properly and is the help forum not accounting for this?