Files
Randell Dawson 1494a50123 fix(guide): restructure curriculum guide articles (#36501)
* fix: restructure certifications guide articles
* fix: added 3 dashes line before prob expl
* fix: added 3 dashes line before hints
* fix: added 3 dashes line before solutions
2019-07-24 13:29:27 +05:30

1.9 KiB
Raw Blame History

title
title
Replacing If Else Chains with Switch

Replacing If Else Chains with Switch


Solutions

Solution 1 (Click to Show/Hide)

We need to change the chained if/else if statements into a switch statement.

Now, we need to comment (// - select all lines and ctrl+/) all chained if/else if statements:

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

Next, we need to create simple switch statement:

switch (val) {
}

and add in this switch statement case - for all if/else if statement (just copy it from our commented code above):

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

Dont forget to use break in each case! Now, we can delete commented code with if/else if statement above.

Heres a full solution:

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;
}
// Change this value to test
chainToSwitch(7);