Basic JavaScript - Selecting from Many Options with Switch Statements

Tell us what’s happening:
Hey everyone! I’ve coded this far and don’t know why it returns “delta” instead of “alpha” ! I’ve read about switch on MDN, and it didn’t explain, why it will return the results that don’t match the criteria, in this case “val” as the argument of the function, whenever “break” keyword is omitted.
It’ll be so kind of you, if you help me with this. Thanks a lot!

Your code so far

function caseInSwitch(val) {
  let answer = "";
  // Only change code below this line

  switch (val) {
    case 1 :
    answer = "alpha";

    case 2:
    answer = "beta";

    case 3:
    answer = "gamma";

    case 4:
    answer = "delta";

  }


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

caseInSwitch(1);

Your browser information:

Challenge: Basic JavaScript - Selecting from Many Options with Switch Statements

Link to the challenge:

Your answer is in your question. You need to include the break keyword after each case. See the example given in the challenge description. The break keyword breaks out of the switch statement whenever a matching case is found.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.