Basic JavaScript: Selecting from Many Options with Switch Statements

Basic JavaScript: Selecting from Many Options with Switch Statements
0

#1

Why is this code wrong if the insturctions tell you to use strict equality ===. I have seen some solutions and they did not use strict equality. Thank you.

function caseInSwitch(val) {
  var answer = "";
  // Only change code below this line
  switch(val) {
  case val === 1:
  return "alpha";
  break;
  switch(val)
  case val === 2:
  return "beta";
  break;
  switch(val)
  case val === 3:
  return "gamma";
  break;
  switch(val)
  case val === 4;
  return "delta"
  break;

  }

  
  // Only change code above this line  
  return answer;  


// Change this value to test
caseInSwitch(1);

#2

#1. Wrong use of switch
Switch syntax works like this

switch(expr) {
    case expr:
        statements
        break or return
    case expr:
        statements
        breark or return
    ...
}

#2 You don’t need both return and break.
Return already exits from the function.

#3 Where is your closing bracelet for the function?


#3

You need to review the lesson information again to view proper syntax of using the switch statement. You will only should reference switch one time and then have one or more case lines.


#4

The case 1 answer needs to equal “alpha” in order for the code to execute. Case 2 answer needs to equal “beta” in order for the code to execute – etc. etc. etc.


#5

Yeah, this challenge is driving me bananas as well, my code looks quite similar to yours, I’ve tried following gunhoo93’s layout/syntax but I feel like there’s some little thing that I’m not seeing.

function caseInSwitch(val) {
var answer = “”;
// Only change code below this line

switch(val) {
case 1:
“alpha”;
break;
case 2 :
“beta”;
break;
case 3 :
“gamma”;
break;
case 4 :
“delta”;
break;}
// Only change code above this line
return answer;
}

// Change this value to test
caseInSwitch(1);


#6

syntax seems right but you are never assigning the answer


#7

Your function returns a variable named answer, but the only value ever assigned to answer is in the first line:

var answer = "";

You need to assign the applicable values to answer and not just write strings after the case statement.


#8

Ahhh, got it, thanks guys, I thought that was just a part of the operation like the return, this is why I ought not take extended coding breaks! Gracias!


#9

Thank you very much for your response. Your feedback is very helpful.


#10

Thank you for your feedback. I will try your approach.


#11

Thank you. Your feedback is very valuable.


#12

Thank you for sharing your code. Looking at your approach is helpful.


#13

Did everything except add this around the code:

switch(val) {

}

thats why it didn’t pass. sigh