* chore(learn): audit basic algorithm scripting * chore(learn): audit basic data structures * chore(learn): audit basic javascript * chore(learn): audit debugging * chore(learn): audit es6 * chore(learn): audit functional programming * chore(learn): audit intermidate algorithms * chore(learn): audit js projects * chore(learn): audit object oriented programming * chore(learn): audit regex * fix(learn): remove stray . * fix(learn): string to code * fix(learn): missed some * fix(learn): clarify strings Based on Randy's feedback, clarifies string instances where quotes were removed in favour of back ticks. * fix: apply suggestions - thanks Randy! :) Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * fix: non-suggestion comments * chore(learn): remove comments from codes Removes the comments from the description and instruction code blocks to ensure that all relevant information is translatable. * fix: Apply suggestions from code review Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * fix: revert crowdin fix * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-algorithm-scripting/mutations.md Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * fix: Apply suggestions from code review Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-arrays.md Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * fix: Apply suggestions from code review Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * chore: change voice * fix: Christopher Nolan * fix: expressions would evaluate * fix: will -> would * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/object-oriented-programming/add-methods-after-inheritance.md Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * fix: to work to push * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-with-javascript-for-loops.md Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/object-oriented-programming/add-methods-after-inheritance.md Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
158 lines
2.6 KiB
Markdown
158 lines
2.6 KiB
Markdown
---
|
|
id: 56533eb9ac21ba0edf2244e0
|
|
title: Replacing If Else Chains with Switch
|
|
challengeType: 1
|
|
videoUrl: 'https://scrimba.com/c/c3JE8fy'
|
|
forumTopicId: 18266
|
|
dashedName: replacing-if-else-chains-with-switch
|
|
---
|
|
|
|
# --description--
|
|
|
|
If you have many options to choose from, a `switch` statement can be easier to write than many chained `if`/`else if` statements. The following:
|
|
|
|
```js
|
|
if (val === 1) {
|
|
answer = "a";
|
|
} else if (val === 2) {
|
|
answer = "b";
|
|
} else {
|
|
answer = "c";
|
|
}
|
|
```
|
|
|
|
can be replaced with:
|
|
|
|
```js
|
|
switch(val) {
|
|
case 1:
|
|
answer = "a";
|
|
break;
|
|
case 2:
|
|
answer = "b";
|
|
break;
|
|
default:
|
|
answer = "c";
|
|
}
|
|
```
|
|
|
|
# --instructions--
|
|
|
|
Change the chained `if`/`else if` statements into a `switch` statement.
|
|
|
|
# --hints--
|
|
|
|
You should not use any `else` statements anywhere in the editor
|
|
|
|
```js
|
|
assert(!/else/g.test(code));
|
|
```
|
|
|
|
You should not use any `if` statements anywhere in the editor
|
|
|
|
```js
|
|
assert(!/if/g.test(code));
|
|
```
|
|
|
|
You should have at least four `break` statements
|
|
|
|
```js
|
|
assert(code.match(/break/g).length >= 4);
|
|
```
|
|
|
|
`chainToSwitch("bob")` should be the string `Marley`
|
|
|
|
```js
|
|
assert(chainToSwitch('bob') === 'Marley');
|
|
```
|
|
|
|
`chainToSwitch(42)` should be the string `The Answer`
|
|
|
|
```js
|
|
assert(chainToSwitch(42) === 'The Answer');
|
|
```
|
|
|
|
`chainToSwitch(1)` should be the string `There is no #1`
|
|
|
|
```js
|
|
assert(chainToSwitch(1) === 'There is no #1');
|
|
```
|
|
|
|
`chainToSwitch(99)` should be the string `Missed me by this much!`
|
|
|
|
```js
|
|
assert(chainToSwitch(99) === 'Missed me by this much!');
|
|
```
|
|
|
|
`chainToSwitch(7)` should be the string `Ate Nine`
|
|
|
|
```js
|
|
assert(chainToSwitch(7) === 'Ate Nine');
|
|
```
|
|
|
|
`chainToSwitch("John")` should be `""` (empty string)
|
|
|
|
```js
|
|
assert(chainToSwitch('John') === '');
|
|
```
|
|
|
|
`chainToSwitch(156)` should be `""` (empty string)
|
|
|
|
```js
|
|
assert(chainToSwitch(156) === '');
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```js
|
|
function chainToSwitch(val) {
|
|
var answer = "";
|
|
// Only change code below this line
|
|
|
|
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";
|
|
}
|
|
|
|
// Only change code above this line
|
|
return answer;
|
|
}
|
|
|
|
chainToSwitch(7);
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```js
|
|
function chainToSwitch(val) {
|
|
var answer = "";
|
|
|
|
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";
|
|
}
|
|
return answer;
|
|
}
|
|
```
|