Variables and return statement...Please help

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
    case 1: return "alpha";
    case 2: return "beta";
    case 3: return "gamma";
    case 4: return "delta";
    default:return "Wrong Input";
// Only change code above this line
return answer;


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

Link to the challenge:

Hi @lost1 !

It gets the answer from this function call

If you run console.log on the function call you should see the word ‘alpha’

If you change that number from 1 to 2 then you should see the word ‘beta’

It is getting the result from val.
When you call the function and pass through 1,2,3, or 4 then it will return those respective answers.

You can try doing console.log on all four options to see the result for yourself.

Hope that makes sense!

1 Like

it is not, return answer is never reached because you have return statements inside the switch

1 Like

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.

1 Like

Thanks alot. I wasn’t thinking straight :joy::joy: :sparkles::sparkles:

I am a foooool. Thanks a lot… It’s all rusted up in my brain :joy::joy::joy: