Tell us what’s happening:
How is the variable answer returning the values from the case statements??? I am confused here because the variable answer is not being used apart from the final return statement.
Your code so far
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:return "Wrong Input";
break;
}
// Only change code above this line
return answer;
}
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.146 Safari/537.36.
Challenge: Selecting from Many Options with Switch Statements
Yes, the variable answer is never used, you could just delete it, and also the final return statement, because that line of code will never run. Same with all the break statements. They are “unreachable code”.
That’s because you return directly from within the switch staements, and remember - after a function has reached a return statement, the function is finished and no more code will run.
You could rewrite your switch statement, and instead of directly returning “alpha” or “beta”, you could assign that to answer, and then return answer at the very bottom.