freeCodeCamp Challenge Guide: Replacing If Else Chains with Switch

freeCodeCamp Challenge Guide: Replacing If Else Chains with Switch
0

#1

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;

}


How is an unrecoverable syntax error fixed?
#2

Try this.

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 “John”:
answer = “”;
break;
case 156:
answer = “”;
break;
default:
answer = “Ate Nine”;
}


#3

Make sure you make “case bob” a string, I spent minutes trying to figure out what was wrong with my code.


#4

One tip:

At first I commented out the original statement given. One thing I notice is that there should really by no Else / If / Else on the editor. Even though those expressions are commented out.

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

Review: https://www.freecodecamp.com/challenges/selecting-from-many-options-with-switch-statements


#5

Try this. It applied for me.

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 “John”:
answer = “”;
break;
case 156:
answer = “”;
break;
case 7:
answer = “Ate Nine”;
break;
}

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

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