Why do we need answer variable and how it links to switch statement

Tell us what’s happening:
why do we need var answer="" as it not related to switch statement and finally “return answer” is present when we have not stored the switch answers in it

Your code so far


function switchOfStuff(val) {
var answer = "";
// Only change code below this line
switch(val){
case "a":
return "apple";break;

case "b":
return "bird";break;
case "c":
return "cat";break;

default "stuff";
break;

}


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

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36.

Challenge: Adding a Default Option in Switch Statements

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/adding-a-default-option-in-switch-statements

So, by having return "apple";, the switch statement and the entire function are exited, and the function returns that value. Because of that, your break; statement is never being used.

But in a real-world scenario, you would almost never simply switch-and-return. Instead, you’d likely use a switch statement to set a value on a variable, then use that variable elsewhere, then return.

What its expecting you to do is, inside of each case, set answer to the value for that letter, and break. Then, after the switch statement, return answer;, sending the result of whichever switch branch we followed.

You’re not wrong in your answer, but that’s why the answer variable is in there. It’s expected that it’ll be used, set to a value, and returned.