From 2ab6d429f596ebae6f58b99192291357c861834e Mon Sep 17 00:00:00 2001 From: greggubarev Date: Tue, 16 Oct 2018 07:46:55 +0400 Subject: [PATCH] Javascript: add hint to Replacing If Else Chains with Switch (#19116) Add hint to Replacing If Else Chains with Switch (https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/replacing-if-else-chains-with-switch/ and https://guide.freecodecamp.org/certifications/javascript-algorithms-and-data-structures/basic-javascript/replacing-if-else-chains-with-switch/) --- .../index.md | 120 +++++++++++++++++- 1 file changed, 113 insertions(+), 7 deletions(-) diff --git a/client/src/pages/guide/english/certifications/javascript-algorithms-and-data-structures/basic-javascript/replacing-if-else-chains-with-switch/index.md b/client/src/pages/guide/english/certifications/javascript-algorithms-and-data-structures/basic-javascript/replacing-if-else-chains-with-switch/index.md index cbb5ab6282..f454a81c70 100644 --- a/client/src/pages/guide/english/certifications/javascript-algorithms-and-data-structures/basic-javascript/replacing-if-else-chains-with-switch/index.md +++ b/client/src/pages/guide/english/certifications/javascript-algorithms-and-data-structures/basic-javascript/replacing-if-else-chains-with-switch/index.md @@ -1,10 +1,116 @@ ---- -title: Replacing If Else Chains with Switch ---- + ## Replacing If Else Chains with Switch -This is a stub. Help our community expand it. - -This quick style guide will help ensure your pull request gets accepted. - + +Here’s the setup: + +```javascript +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); +``` + +We need to change the chained ```if/else if``` statements into a ```switch``` statement. + + Here’s a solution: + + Now, we need to comment (```//``` - select all lines and ```ctrl+/```) all chained ```if/else if``` statements: + + ```javascript + // 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: + + ```javascript + switch(val) { + } + ``` + + and add in this ```switch``` statement ```case``` - for all ```if/else if``` statement (just copy it from our commented code above): + + ```javascript + 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. + + Here’s a full solution: + + ```javascript + 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); + ```