Switch case syntax

Hi,

Thanks in advance for your help.
I am working on the “Selecting Many Options With Switch Case” problem. I am having a lot of trouble getting my code to pass the tests.

I am certain that I understand the logic. I believe that the problem is syntax. I have searched the forums and the web for examples of correct syntax. I am trying to use the correct syntax, but the console won’t accept it. I can modify the case syntax to something that the console will accept, but then it refuses to accept a “break ;” statement.

First, the logic. In a switch function a value is passed to the function with a variable. The value is compared in sequence top to bottom by strict equivalence. When a match is found, the code for that specific instance is executed then, a break statement should be executed. I know that every case does not have to include a break statement, and I know that not including a break statement can be used to group cases.

Second, the code:

switch(val){

 var answer = " ";

case 1:
  answer = "alpha";
  break;
case 2:
  answer = "beta";
  break;
case 3:
  answer = "gamma";
  break;
 
  answer = "delta";

return answer;
}

Assuming I sent a numeric value to switch(val), a value of 1,2 or 3 should return “alpha”, "beta "or “gamma” respectively. Anything else should return “delta”.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

The declaration, assignment and return of the answer variable, should not be inside the switch statement. Also you are missing the last case.

Hey lasjorg,

Thanks for the heads up. That must be why the switch function is nested. Man, I can be dense.

Thanks Again,
George