Q:

Replacing If Else Chains with Switch

function chainToSwitch(val) {
  var answer = "";

  if (val === "bob") {
    answer = "Marley";
  } else if (val === 42) {
    answer = "The Answer";
  } else if (val === 1) {
    answer = "There is no #1";
  }
  
  // and now with switch
  switch(val){
    case "bob":
      answer = "Marley";
      break;
    case 42:
      answer = "The Answer";
      break;
    case 1:
      answer = "There is no #1";
      break;
  }

  return answer;
}
0

Tags

New to Communities?

Join the community