Using an array with a switch condition

Tell us what’s happening:
Hi

Is it possible to use an array with a switch statement. And what is the difference between a switch and if statement?

Thanks

trying to google it but cant really find anything I can understand at this stage./
Your code so far



function caseInSwitch(val) {
var arr = ["alpha", "beta", "gamma", "delta"] 
 
 // Only change code below this line
 switch(val){
   case 1:
   return arr[0]
      console.log(arr);
      break;
   case 2:
   return "beta"
     console.log(answer);
     break;
   case 3:
   return "gamma"
     console.log(answer)
     break;
   case 4:
   return "delta"
   console.log(answer)
 // Only change code above this line
 
 }
}

caseInSwitch(1);
   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 Edg/88.0.705.68.

Challenge: Selecting from Many Options with Switch Statements

Link to the challenge:

Sure, your case “1” is even a much better approach, because you have an array with possible return values availabe, and it’s better to return arr[0] instead of hardcoding “alpha”.

Note: You don’t need the “break” statements, because whenever a function meets a return statement (which it does in all cases), it stops executing and won’t check any more cases. You might have noticed that none of your console.log statements actually log something. This is why - the function has already returned.

The difference between an if statement and a switch:

  • a switch is basically one long if-else chain, where you only check one value for a certain condition
  • in an if statement, you can have multiple conditions like if (a === b && b > c), that is not possible with a switch
1 Like

Just an FYI. One of the challenge requirements is at least 3 break statements. It’s just because it was set up to use a variable assignment that is returned at the end and not using returns inside the case(s). You can pass it just fine using returns but you still have to have the break (even though it’s unreachable code).

Hi
Thanks for getting back. That’s actually really helpful. I’m a complete beginner been coding for about two weeks and wouldn’t have known about the proper use of the return statement. Should I have?

I have used answer = “” in all but alpha and used a console below each case. But instead used a return under the 1st case, alpha. When I hit run I’m getting the 2nd, 3rd and 4th but not the first. I have breaks in. There is no change when I take the break away from the first case.

Can clarify whats going on here please? I thought that with the return the first case would be the only one that was processed.

If you hit “run”, then you can tell from the tests in the left panel that these four function calls are happening:

  • caseInSwitch(1) → function goes into case 1 and returns arr[0] directly from there. Nothing gets logged.
  • caseInSwitch(2) → function goes into case 2, reassigns answer and logs. At the end, answer is returned.
  • caseInSwitch(3) → function goes into case 3, reassigns answer and logs. At the end, answer is returned.
  • caseInSwitch(4) → function goes into case 4, reassigns answer and logs. At the end, answer is returned.

If you only want to know what happens when one specific function call happens (say you want to test what happens if you call caseInSwitch(2)), then don’t click “run”, but wrap the function call in a log like this: console.log(caseInSwitch(2)). It will log a) what this particular function call returns and b) whatever you log inside your function.

Thanks very much. That makes sense to me.