Replacing if chains with switch

Tell us what’s happening:
What am why is there errors under the case words? this happens during the last couple of lessons.

   **Your code so far**

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

 if (val) {
   case "bob":
   answer = "Marley";
  case 42:
   answer = "The Answer";
case 1: 
   answer = "There is no #1";
 case 99: 
   answer = "Missed me by this much!";
 case 7: 
   answer = "Ate Nine";

 // Only change code above this line
 return answer;

   **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.2 Safari/605.1.15

Challenge: Replacing If Else Chains with Switch

Link to the challenge:

I know its probably a syntax error, but I dont get what. I even looked at the answer from the hint section and its the same, I copied and pasted it but the little red lines were still there.

This is not a switch statement, it’s an if statement. Reread the lesson again carefully and look at how they replaced an if with a switch.


I strongly recommend against getting the the habit of copying solutions. Doing so typically leads to understanding less and less until you can only get the checkmarks by copying answers. This becomes a problem for the projects, as there are no posted solutions to copy.

1 Like

Hey GoodNGo,

Your code is doing a mix of an if statement and switch. You have to use switch in the challenge so if shouldn’t be showing up in your code.

1 Like

I don’t usually do that I was just trying to see if it would get the same error.

Just woke up fixed it, I thought I already changed it before changing the other code. everything seems to be ok but I have a little red line under the semi colon for chainToSwitch(7); no other errors are showing up in the console, just the end bracket and the chainToSwitch thing

apparently I had to add another end bracket, I thought the one underneath the return bracket was the ending one.

Lots of people “don’t usually do that” but find themselves checking and copying the answers more and more until they can’t do any challenges on their own. It’s worth being aware of the big risk.

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