Switch statements (few Q's)

QUESTION 1:
why can’t we:
A) combine a switch statement an with if statement ? or
B) use comparison operators like greater than inside a switch statement ?
examples :

FOR EXAMPLE 1(A)

case 1: case 2: case 3:
answer = “Low”;
break;
else if (case >= 4) { //else if case 4 is >= 4
return “this”;
}

FOR EXAMPLE 1(B)

case 1: case 2: case 3:
  answer = "Low";
  break;
  
  case >= 4:   //for a case >=  4
answer = "even higher";

or is there another way to do that?

QUESTION 2
why can’t we use the:
A) a return function instead of having to change a local variable ?
B). console.log() function instead of a local variable ?

for example:
FOR EXAMPLE 2(A)

function caseInSwitch(val) {
let answer = “”; // we do not use this local variable
switch (val) {
case 1:
//answer = “alpha”; // and this
return “alpha”; // but only use this
}
}

FOR EXAMPLE 2(B)
refer to above example, only this time replace the return statement with console.log(“alpha”);

The point of a switch is to match one expression against multiple possible values:

switch (value) {
  case option1:
    break;
  case option2:
    break;
}

‘the return function’ isn’t a thing. There is a return statement. You could either use a return statement or update the local variable which is returned. Either work, but the function was set up for the second.

1 Like

You can, this is just how the challenge is set up

Console.log write stuff to the console, doesn’t return it, you can use it if you don’t want to return something from the function, not fine for this challenge as you need to return a value

2 Likes