Selecting from many options with Switch Statements--can't understand how to get the answer

Hi, this is my code:

function caseInSwitch(val) {
var answer = “”;
// Only change code below this line
switch (val) {
case “alpha”:
break;
case “beta”:
break;
case “gamma”:
break;
case “delta”:
break;
default:

// Only change code above this line
return answer;
}

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

It’s not passing and I cannot understand how to get the answer. What is the answer? Can someone give me the answer to this problem? Thanks

You are not returning anything.

Can you write out the code so that it’s more understandable to me? Thanks.

You should modify answer. If you get 1 as input you should set answer to ‘alpha’, if 2 to beta etc.

Sorry, I need someone to write out the code. That’s the only way I can truly understand. I don’t understand what you’re explaining.

deleted See below for correct response

I tried that and it still doesn’t pass.

Oh, sorry, I had to go back and look at the challenge; I had forgotten how it worked! Case 1 should return “alpha”. You will need to change your code in the switch(val) to set the answer to “alpha” if the case is 1, and so on. Hope that helps!

I changed it to this but I still get 3 yellow triangles as errors:

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

// Only change code above this line
return answer;
}
}
// Change this value to test
caseInSwitch(1);

You can ignore yellow triangles. They are warnings not errors.

If you like, you can remove break; from your code, because you use return.

This is how I did the problem. A few different ways to solve, but this solution helps the answer make more sense I believe. You have to remember the end goal is getting the answer to return something. Have to make sure it is getting stored to answer.

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

default:

}

// Only change code above this line
return answer;
}

2 Likes