Replacing If Else Chains with Switch break error and else if error HELP

**Tell us what’s happening:**i keep getting this error

Your code so far

function chainToSwitch(val) {
  var answer = "";
  // Only change code below this line
  
  if (val === "bob") {
    answer = "Marley";
  } else if (val === 42) {
    answer = "The Answer";
  } else if (val === 1) {
    answer = "There is no #1";
  } else if (val === 99) {
    answer = "Missed me by this much!";
  } else if (val === 7) {
    answer = "Ate Nine";
  }
   }
  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";
  // Only change code above this line  
  return answer;  
}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36.

Link to the challenge:
https://www.freecodecamp.org/challenges/replacing-if-else-chains-with-switch

Most of your code looks fine in the switch statement, but there are a few things that need to be changed:

  • remove the if and else if statements, as the instructions on the problem state: “Change the chained if/else if statements into a switch statement.”
  • a closing } at the end of the switch statement is needed

Screen Shot 2018-01-27 at 4.12.53 PM

I keep getting these 2 red Errors. Any suggestions?
Thanks a lot.

Try removing all the if and else if statements, now that you have a switch block instead.

Thank you. I tried it and it helped with the code.

1 Like

solution is:
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”;
}

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

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

Program actually looks good but the only hindrance is the omission of the closing brace after the default statement.