Selecting with Switch statements, Please help

Tell us what’s happening:
Sorry, I’m confused. Again. Help!

  **Your code so far**
  function caseInSwitch(val) {

function caseInSwitch(val) {
let answer = "";break;
// Only change code below this line
caseInSwitch(1):
let answer="alpha";
break;
caseInSwitch(2):
let answer="beta"; 
break;
caseInSwitch(3):
let answer="gamma";
break;
caseInSwitch(4):
let answer="delta";
break;

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

caseInSwitch(1);
caseInSwitch(2);
caseInSwitch(3);
caseInSwitch(4);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36

Challenge: Selecting from Many Options with Switch Statements

Link to the challenge:

You can’t re-declare variables like this and get the outcome you are expecting.

Using let again, this line says, ‘create a new variable called answer, set it to “alpha”’. But you already have a variable named answer inside of the function, so you should get an error.

Make sure and only change the code between the green lines that say so. It will help a lot!

function caseInSwitch(val) {
  let answer = "";
  // Only change code below this line
  switch(val) {
    case "1":
      answer = "alpha";
      break;
... and continue with this pattern....
  }


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

caseInSwitch(1);

I did, Still not working. I replaced case “1” with caseInSwitch(1) like the quiz said to do, it won’t even go through.

You can leave the code as I posted it above, just add the rest of the case statements below the first one.

see my code so far. :smiling_face_with_tear:

function caseInSwitch(val) {

  let answer = "";

  // 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;

}

  // Only change code above this line

  return answer;

}

caseInSwitch(1);

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 it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

The test:

caseInSwitch(1) should have a value of the string alpha

Is it passing in a number or a string?

When I fix that, your code passes for me.

1 Like

SOOOOOOOOO, I finally figured my mistake out. Thank you everyone!

1 Like

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