I’m new to writing switch statements after learning about it yesterday.
For some reason, this isn’t working.
checkCase(2);
checkCase(1);
checkCase(0);
function checkCase(priorityType){
switch(priorityType){
case 2:
print(priorityType);
break;
case 1:
print(priorityType);
break;
case 0:
print(priorityType);
break;
}
}
The ‘alert(2)’ is triggered, 1 and 0 are not.
I’ve reversed the case 2: with case 1: and run the code again and 2 is once again triggered, 1 is not.
I’ve also tried adding break; and continue; to the cases, but still nothing.
Why is that? what have I done wrong?
———————-EDIT—————————
Lots of responses saying that I need to add ‘break;’ which I’ve now done to each line.
Still no output. I’ve also changed ‘alert’ to ‘print’. No difference.
————-edit2———————–
my bad, the ‘break’ is working now. not sure what was going on when I checked last. Maybe needed to restart ff.
Two problems: Debugging using
alertis problematic and you needbreakstatements.Try adding
breaks and usingprintin the square free shell, and I think you’ll see the proper result.