Problem with if / else if challenge - help request

Tell us what’s happening:

Hey-- have gone around and around with this one but still having a error-- could use some debug help-- here is my code:


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;

    default:

    answer= "Value not found!";

    break;

/////////////////////////////////////////////

  // Only change code above this line 
}
// Change this value to test
chainToSwitch(7);
}

– && here is my error message:
// running tests

chainToSwitch("bob")

should be “Marley”

chainToSwitch(42)

should be “The Answer”

chainToSwitch(1)

should be “There is no #1

chainToSwitch(99)

should be “Missed me by this much!”

chainToSwitch(7)

should be “Ate Nine”

chainToSwitch("John")

should be “” (empty string)

chainToSwitch(156)

should be “” (empty string) // tests completed
– would be grateful for any help at all && thanks.

Your code so far


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;
   default:
   answer= "Value not found!";
   break;
 
/////////////////////////////////////////////
 // Only change code above this line 
}

// Change this value to test
chainToSwitch(7);
}


Your browser information:

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

Challenge: Replacing If Else Chains with Switch

Link to the challenge:

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 (’).

Hi @markpouncey!

A few things.

  1. Make sure to only change the code where it tells you to. Right now your function isn’t returning anything. So you need to address that issue first.

  2. You actually don’t need the default in this challenge. If we call the function and pass through john or 156 it will return an empty string.

  3. Something is missing at the end of the switch statement. Once you fix that syntax error as well as the other two things the test should pass.

Hope that helps!

Hey-- disregard-- I got it to work finally-- was missing a Mustache bracket to close out my switch statement. Thanks && kind regards, mark-p. :slight_smile:

Also-- for those who (like me) need to go line for line through the solution-- the Beau Carnes freecodecamp youtube video time stamp for that is: “1:39:20” @ “https://www.youtube.com/watch?v=PkZNo7MFNFg” && hope this helps.