Replacing If Else Chains with Switch (Stuck)

Tell us what’s happening:

What am I doing wrong?

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= ""
    break;
  
  // Only change code above this line  
  return answer;  
}

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

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/replacing-if-else-chains-with-switch

@meredithkirby bob is not a string. That is the problem.

1 Like

I still don’t get it. What do I need to do to bob?

@meredithkirby look at the code you posted. Marley is a string and bob is not.

val is a variable, and by the code you’ve shown, bob is as well. "Marley", however, is a String, not a variable. Do you see a difference?

I tried it with “bob” and ‘bob’, with those I get an error that says “Maximum call stack size exceeded”

Yeah, that was apparently only the first part of a bigger problem.

  1. after the last option in your switch statement (in this case, the default block), you need to close the switch brackets. As it is, the return is never happening.
  2. In a related issue, remove the bracket at the END of the code.

Using your original change, moving that closing-bracket, and changing bob to "bob", the code passes fine.

Pesky bracket! Passing fine now, thank you.

1 Like

EASY gotcha to run into. Glad I could help.