Continuing the discussion from freeCodeCamp Challenge Guide: Replacing If Else Chains with Switch:
function chainToSwitch(val) {
var answer = "";
// Only change code below this line
switch(val) {case "bob":
answer = "Marley";
break;
case 42:
answer = "The Answer";
break;
case 1:
answer = "There is no #1";
break;
case 99:
answer = "Missed me by this much!";
break;
default:
answer = "Ate Nine";}
}
// Only change code above this line
return answer;
The above is my code. Below is the answer from the camper bot:
switch(val){
case “bob”:
answer = “Marley”;
break;
case 42:
answer = “The Answer”;
break;
case 1:
answer = “There is no #1”;
break;
case 99:
answer = “Missed me by this much!”;
break;
case 7:
answer = “Ate Nine”;
break;
}
My own answer seems exactly the same as the above, at least as far as I can tell.
What is the syntax error you are getting?
unrecoverable syntax error 76% at the return answer part of the code…
I have no idea what you mean.
I think I’m making a syntax error. Are you able to spot it?
What makes you think you are getting a syntax error? Are you seeing an error in the console?
If not, what tests are failing?
I’ll take a screenshot, just hold on.
Look at your opening and closing curly braces ({
and }
) to see if they match up.

now I’m getting Syntaxerror: Illegal return statement
any suggestions?
Your return statement is not inside a function.
I’m not sure what I still need to check off on the check boxes. I don’t know what I’m doing wrong. I’ve made the changes about including return inside the function.
Because you have a default
that returns "Ate Nine"
, the last two tests are failing. chainToSwitch("John")
and chainToSwitch(156)
both return "Ate Nine"
.
how do I address “John” in the case statement format. Can you give an example?
Why do you have "Ate Nine"
in a default
case? Did the instructions say that it should be the default?
That doesn’t say anything about always returning "Ate Nine"
for all values that are not "bob"
, 42
, 1
, or 99
.
At John and 156 the program is still returning error. As far as I can tell, I cant see a syntax error.
function chainToSwitch(val) {
var answer = "";
// Only change code below this line
switch(val) {case "bob":
answer = "Marley";
break;
case 42:
answer = "The Answer";
break;
case 1:
answer = "There is no #1";
break;
case 99:
answer = "Missed me by this much!";
break;
case 7:
answer = "Ate Nine";
break;
case "John":
"";
break;
case 156:
"";
// Only change code above this line
return answer;
}
"";
is not a valid line of code.
1 Like