freeCodeCamp Challenge Guide: Multiple Identical Options in Switch Statements

Multiple Identical Options in Switch Statements


Solutions

Solution 1 (Click to Show/Hide)
function sequentialSizes(val) {
  var answer = "";
  // Only change code below this line
  switch (val) {
    case 1:
    case 2:
    case 3:
      return "Low";
      break;
    case 4:
    case 5:
    case 6:
      return "Mid";
      break;
    case 7:
    case 8:
    case 9:
      return "High";
      break;
  }
  // Only change code above this line
  return answer;
}
// Change this value to test
sequentialSizes(1);
Solution 1 (Click to Show/Hide)
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);

Code Explanation

Since you already have a variable named answer defined and the function returns it, you can just modify its value on each group of case statements to fit the exercise requirements.

Relevant Links

18 Likes

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.

7 Likes

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?

anyone know how to solve this

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

1 Like

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

2 Likes

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.

1 Like

remove the quotes in the cases

1 Like

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