I was doing the section “Selecting from many options with Switch Statements” and the instruction was not that clear. Eventually I resort to the getting a hint section and came to the forum that was now closed for further posts.
I got the answer but I still don’t understand the purpose of
var answer = “”; // at the beginning
and
return answer; // at the end
I deleted the beginning code >>> var answer ="";
and the switch function still works.
By the way, why is there no variable inside the double quotes? Does this mean that answer can be equal to anything? And why does the switch function still work without this statement?
However, the important code to include for this switch function is >>> return answer;
why do we need this statement here?
function caseInSwitch(val) {
// Only change code below this line
switch (val){
case 1:
answer = "alpha";
break;
case 2:
answer = "beta";
break;
case 3:
answer = "gamma";
break;
case 4:
answer = "delta";
break;
default: // optional, the switch function will work without the default
answer = "statement you want to appear if val is not between 1-4";
break;
}
// Only change code above this line
**return answer;**
}
// Change this value to test
caseInSwitch(1);
Thank you very much for your help.