Basic JavaScript - Multiple Identical Options in Switch Statements

Tell us what’s happening:
I am not having an issue with the solution to this challenge, moreover, I was playing around and thought that I had another solution by expressing the case ranges using
conditional and logical operators.

I can’t figure out exactly why it does not work. Stepping through the block in VS Code, shows that it goes straight to the final return statement, bypassing all of the cases. I’ve tried adding “return answer =” to each case and other minor permutations.

Am I using the syntax in the case statements wrong. Is this even possible, and if so, how?

Your code so far

function sequentialSizes(val) {
  let answer = "";
  // Only change code below this line
  switch (val) {
    case (val >= 1 && val <= 3):
      answer = "Low";
      break;

    case (val >= 4 && val <= 6):
      answer = "Mid";
      break;

    case (val >= 7 && val <= 9):
      answer = "High";
      break;
  }
  // Only change code above this line
  return answer;
}

console.log(sequentialSizes(3));

Challenge: Basic JavaScript - Multiple Identical Options in Switch Statements

Link to the challenge:

It doesn’t work because those boolean expressions in your cases evaluate to either true or false, so if val is a number then it isn’t true or false and thus none of the cases will be tripped, which is why you are getting an empty string for the return value.

If you called the function as sequentialSizes(false) then the first case will trip because the boolean expression will return false and thus it will be equal to the value in val. Same will happen if you call it as sequentialSizes(true).

if you wish to use switch statement, with logic comparison at case statement.

You could do this:

 switch (true) {
    case (  ... && ...):

    case ( ... && ...):

    default:

 }

You could try something simple like the one in example , because i dont think this way could pass the fcc last criteria:

You should have nine case statements

Please don’t do this though. You can but it’s super funko

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.