Files
Kristofer Koishigawa bcc9beff1f feat(curriculum): introduce let and const earlier (#43133)
* fix: move "Explore Differences Between..." to basic JS, update seed and tests

* fix: resequence "Declare String Variables"

* fix: move "Declare a Read-Only Variable..." to basic JS, update seed and tests

* fix: revert changes to non-English "Explore Differences Between..." test text

* fix: revert test strings, solutions, and seeds for non-English challenges

* fix: update "Declare String Variables" description

* fix: sync quotation marks in description and seed

* fix: modify note in "Declare a Read-Only..." challenge

* fix: update operator and compound assignment challenges

* fix: update string challenges

* fix: update array and array method challenges

* fix: update function and scope challenges, resequence slightly

* fix: "Word Blanks" solution

* fix: add spacing to seed

* fix: concatenating += challenge spacing

* fix: appending variables to strings spacing

* fix: find the length of a string spacing

* fix: removed instances of removedFromMyArray = 0

* fix: switch challenges

* fix: function argument and param spacing

* fix: update counting cards, object challenges, and record collection

* fix: finish rest of Basic JS section

* fix: introducing else statements solution

* fix: update spacing and wording

* fix: update wording for const challenge

* fix: update functional programming challenges

* fix: intermediate algorithms and cert challenges

* fix: revert some spacing and remove comments for fp challenge solutions

* feat: add notes with links to moved let and const challenges in first two es6 challenges

* fix: update es6 intro text

* Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/concatenating-strings-with-the-plus-equals-operator.md

Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>

* Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/finding-a-remainder-in-javascript.md

Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>

* Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/global-scope-and-functions.md

Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>

* Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-through-an-array-with-a-for-loop.md

Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>

* Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-through-an-array-with-a-for-loop.md

Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>

* Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/functional-programming/implement-map-on-a-prototype.md

Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>

* Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/declare-a-read-only-variable-with-the-const-keyword.md

Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>

* fix: concatenating strings with plus operator seed

* fix: add comments back to Declare a Read-Only Variable... seed

* feat: add es6 to basic javascript redirect tests for let and const challenges

* fix: revert "Concatenating Strings with Plus Operator" seed

* fix: move test file to cypress/integration/learn/redirects, separate redirect tests

Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>
2021-10-25 17:55:58 +01:00

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) {
let 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) {
let 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;
}
```