Selecting from many options with Switch Statements

My code successfully passed the test, but I also get an error code in the text editor. My code (in between the comments) is below. I have four yellow triangles that state “Unreachable ‘break’ after ‘return’.” Is this normal, or have I done something wrong?

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”);
break;

}

// Only change code above this line
return answer;
}

// Change this value to test
caseInSwitch(1);

Hey, normally break; is used to “break out” of the switch statement. In your case, you stop executing the switch statement by using the return key word. So the break; is not necessary here.

2 Likes

Thanks @BenGitter
I also did things a little differently on the next challenge and realised instead of using return I could (should?) have used answer = “alpha”;, and then I could have used break as the challenge requires :slight_smile:

1 Like

It’s worthwhile noting that using a return in a case statement, is the only good reason to omit the break;
In any other case, where you’re not exiting the switch statement with a return, the break should always be there.

1 Like