Code works when it shouldn't

on this lesson

I used this code

function caseInSwitch(val) {
  let 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;
}
  // Only change code above this line
  return answer;
}

caseInSwitch(1);

I don’t think it should pass, I checked the solutions and it doesn’t match and it doesn’t make much sense that it passed either. Please let me know in either case as it will help me better understand what’s happening. Thank you!

Why shouldn’t it pass? The function has identical behavior to the solutions given?

its returning the answer but its not being assigned to the variable, or am I missing something?

Well, the instructions do say “and sets answer for the following conditions”, so I think the spirit of the challenge is to use the answer variable instead of returning the answer directly from the switch.

Should we care though? That feels a bit too rigid?

That’s what I’m noticing and if it passes like this then it doesn’t really show the learner (me) that I had done it incorrectly to what was being asked. Am I wrong, honestly its still a little confusing for me.

I would tend to agree, why should we care. So maybe the code shouldn’t be seeded with the answer variable to begin with? And of course change the instructions to get rid of a reference to it.

Also, the tests are requiring three breaks, so we would want to remove that requirement as well because you don’t need them if you are returning on each case.

I was just confused when it passed so I looked at the solutions and noticed mine wasn’t aligned. So it further confused me which led me to posting here. I thought maybe there is something I’m missing.

Those are just possible solutions, yours does not have to match any of those.

Okay thank you for the clarification and keep up all the great work being done here! Have a great day :smiley:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.