I have a question concerning using switch in a function. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I have a question concerning using switch in a function.

Soooo my question is why do I need to declare a local variable named "answer" with an empty string set as its value and then have a return statement at the end for answer? does this question make sense? because when I omit the local variable and the return statement with "//" the code still works. I can assign caseInSwitch value to another variable and I can call on my function to show me that my code is running fine. I dunno, if anyone can answer this great, if not..ill find out eventually. function caseInSwitch(val) { //var answer = ""; switch(val) { case 1: return "Nelson"; break; case 2: return "Nathaniel"; break; case 3: return "William"; break; case 4: return "Leroy"; break; case 5: return "Clyde"; break; case 6: return "Fanel"; break; } // return answer; } var storedName = caseInSwitch(1);

4th Mar 2019, 3:27 AM
Nelson Graham
Nelson Graham - avatar
1 Answer
0
I believe its because I have return statements in my case statements. Im assuming thats why.
4th Mar 2019, 3:50 AM
Nelson Graham
Nelson Graham - avatar