I don't understand how the answer variable links to this switch statement

I don’t understand how the answer variable is linking to the switch statement. An explanation will be highly appreciated. Thank you in advance

The code


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);
sequentialSizes(2);
sequentialSizes(3);
sequentialSizes(4);
sequentialSizes(5);
sequentialSizes(6);
sequentialSizes(7);
sequentialSizes(8);
sequentialSizes(9);


In this case it doesn’t, but seeing the structure of the thing, the expected code would have had a answer = "Low" or the other things, instead of a return statement for each case.

In the way you have used it you don’t even need the break because the return already do that

Note that the challenge description ask you to set answer

Write a switch statement to set answer for the following ranges:

1 Like

Adding on to what @ilenia has said - You must set the value of the answer variable for each case in which its value is meant to change.
Example:

switch(val) { 
 case 1: 
 case 2: 
 case 3: 
  answer = "Low"; 
  break; 
... All the other cases
}

@j8ahmed and @leahleen that’s what I thought but when I had answer = “” in the case it didn’t work and had to change it to return “” for it to work

If you put these in the right places it will work, which is also what is being asked to do. If you don’t do this you have useless pieces of code around (like the return answer at the end of the function)
answer = 'Low';
answer = 'Mid';
answer = 'High';

Try to solve it in this way, if it doesn’t work post here your code and we will help you figure out what’s wrong

1 Like