Switch statment lesson

Hi guys, I am at this lessson.

I was a bit frustrated why my solution wasn’t working so i checked, and changed to return

switch(val) {
  case 1:
  return "alpha";
break;

case 2:
return "beta";
break;

case 3:
return "gamma"
break;

case 4:
return "delta";
break;

}

at first it had console.log() instead of return, I am wondering why the console.log answer wouldn’t let me pass the test, when it does what the lessons asks me of.

Thanks!

Because it doesn’t do what the lesson asks of you :wink:
Even your current solution is not actually doing what the task is.
You were supposed to take the given code and change the value of the variable “answer” within the “only change code here” comments.

You didn’t do that here.
Because “answer” would get returned, by just directly returning the value, the function is doing the same as the original function intended.
BUUUT you changed code you weren’t supposed to. And ofcourse console.log() doesn’t even return anything, so that’s why ist was failing.

1 Like

console.log() just prints out whatever you want to print out. console.log() does absolutely nothing else with that information.

But, the instructions say

Write a switch statement which tests val and sets answer for the following conditions:

A console.log() doesn’t set anything. In this case, the value of answer is returned to the ‘calling context’ (a fancy way to say the value is returned to here:

function caseInSwitch(val) {
  let answer = "";
  // Only change code below this line



  // Only change code above this line
  return answer;
}
// Value is returned here \/
caseInSwitch(1);

But you didn’t return the set value, so you didn’t meet the requirements of the instructions.

1 Like

I haven’t changed the code I wasn’t supposed to, i just gave you a snippet of it.
But anyway I understood that console.log() just prints something out.

Agreed, you didn’t, but you sort of violated the intent of the challenge:

"Write a switch statement which tests val and sets answer"

You aren’t setting the variable answer and then allowing the return answer statement at the end to return the answer.

Your answer obviously works, it’s just not written the way the instructions asked. Also, you don’t need to break after each return because return stops the function immediately so you will never reach the break. Also, you would get rid of the return answer at the end since it will never be reached as well.

I understand,

switch (val) {
  case 1:
  answer = "alpha";
  break;
  case 2:
  answer ="beta";
  break;
  case 3:
  answer ="gamma";
  break;
  case 4:
  answer ="delta";
  break;
}

I assume, that is the proper solution to the lesson yes?

1 Like

Oh ok, yeah that makes sense, sorry for the wrong assumption ^^°

Yes, exactly.
Bonus: it’s recommended to avoid “returns” in the middle of the function unless it’s unavoidable for whatever reason. Because imagine the function is not just 10, but 50-100 lines of complex code. Suddenly it’s lot harder to keep track of what it is doing, if there would be several returns sprinkled about for all kinds of different reasons.

1 Like