I had a quick question to clarify and help me understand switch statements . I was going through the beginners Javascript module and ran across this code:
function caseInSwitch(val) {
var answer = "";
switch(val){
case 1:
answer = "alpha";
break;
case 2:
answer = "beta";
break;
case 3:
answer = "gamma"
break;
case 4:
answer = "delta"
break;
}
return answer;
}
The way I understand it, is that we create a function that asks for val. In that function we then create a variable named answer and then use a switch, to assign answer a string, and at the end we return our variable string, which was obtained through a case.
So for the switch statement, it takes the input here from val, runs through the cases, and once it finds a match it stops with the break; statement, and then skips the rest and finishes with the return answer
So within each case, it’s changing the variable answer 's value to whatever the new string is?
Sorry if this is simple! I’m very new and it’s my first post.