Replacing If Else Chains with Switch ReferenceError

Tell us what’s happening:

It seems that my code were all right but I have this error.
ReferenceError: bob is not defined

Let me know if you have an answer for this, Thanks :slight_smile:

  **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";
  }

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

console.log(chainToSwitch(bob));

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;
}

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

console.log(chainToSwitch(bob));

  **Your browser information:**

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

Challenge: Replacing If Else Chains with Switch

Link to the challenge:

Hey there,

nice to meet you! :wave:

I edited your code for readability.

The error ReferenceError: bob is not defined means that you try to do something with a variable named bob.

In your case, bob shouldn’t be a variable, but a string value "bob".

bob as a variable: var bob = "something";
"bob" as a string value: var name = "bob";

1 Like

The variable bob was never initialized.
Maybe you want to check with the string "bob"?

1 Like

Hi thanks for helping out, but making bob a string “bob” doesn’t work as well.

Hi thanks for the help, I tried it but didn’t work out.

Ohhh this actually works :slight_smile: I just change the bob inside the console.log to “bob” and it worked :slight_smile: thanks for the help