Basic JavaScript: Selecting from Many Options with Switch Statements problem

I think that when you use return in a switch statement you should call the function with console.log. In this lesson, the pre-written statements use return in the switch statements and just call the function without console.log. I ran the solution through vscode and found that it won’t output any words unless you put console.log when you call the function out or put console.log instead of return in the switch statements. I believe the pre-written return statement should be surrounded by a console.log just to make sense.

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";
}

// 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/83.0.4103.97 Safari/537.36.

Challenge: Selecting from Many Options with Switch Statements

Link to the challenge:

What is your question exactly? You really don’t want, in general, your function to log in the console every single time it returns. That would make code insanely slow.

2 Likes

I was just wondering what the code was for if it doesn’t return anything in the output. But I came to the realization that it could be used for other things than just logging it into the console.

1 Like

Ah. Yeah, its a bit weird that caseInSwitch(1); is called but the output is not captured or used.

let result = caseInSwitch(1);

or

console.log(caseInSwitch(1)):

would be more typical.

1 Like