Files
.github
api-server
client
config
curriculum
__fixtures__
challenges
_meta
chinese
english
01-responsive-web-design
02-javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
basic-javascript
access-array-data-with-indexes.md
access-multi-dimensional-arrays-with-indexes.md
accessing-nested-arrays.md
accessing-nested-objects.md
accessing-object-properties-with-bracket-notation.md
accessing-object-properties-with-dot-notation.md
accessing-object-properties-with-variables.md
add-new-properties-to-a-javascript-object.md
add-two-numbers-with-javascript.md
adding-a-default-option-in-switch-statements.md
appending-variables-to-strings.md
assigning-the-value-of-one-variable-to-another.md
assignment-with-a-returned-value.md
build-javascript-objects.md
chaining-if-else-statements.md
comment-your-javascript-code.md
comparison-with-the-equality-operator.md
comparison-with-the-greater-than-operator.md
comparison-with-the-greater-than-or-equal-to-operator.md
comparison-with-the-inequality-operator.md
comparison-with-the-less-than-operator.md
comparison-with-the-less-than-or-equal-to-operator.md
comparison-with-the-strict-equality-operator.md
comparison-with-the-strict-inequality-operator.md
comparisons-with-the-logical-and-operator.md
comparisons-with-the-logical-or-operator.md
compound-assignment-with-augmented-addition.md
compound-assignment-with-augmented-division.md
compound-assignment-with-augmented-multiplication.md
compound-assignment-with-augmented-subtraction.md
concatenating-strings-with-plus-operator.md
concatenating-strings-with-the-plus-equals-operator.md
constructing-strings-with-variables.md
count-backwards-with-a-for-loop.md
counting-cards.md
create-decimal-numbers-with-javascript.md
declare-javascript-variables.md
declare-string-variables.md
decrement-a-number-with-javascript.md
delete-properties-from-a-javascript-object.md
divide-one-decimal-by-another-with-javascript.md
divide-one-number-by-another-with-javascript.md
escape-sequences-in-strings.md
escaping-literal-quotes-in-strings.md
find-the-length-of-a-string.md
finding-a-remainder-in-javascript.md
generate-random-fractions-with-javascript.md
generate-random-whole-numbers-with-javascript.md
generate-random-whole-numbers-within-a-range.md
global-scope-and-functions.md
global-vs.-local-scope-in-functions.md
golf-code.md
increment-a-number-with-javascript.md
initializing-variables-with-the-assignment-operator.md
introducing-else-if-statements.md
introducing-else-statements.md
iterate-odd-numbers-with-a-for-loop.md
iterate-through-an-array-with-a-for-loop.md
iterate-with-javascript-do...while-loops.md
iterate-with-javascript-for-loops.md
iterate-with-javascript-while-loops.md
local-scope-and-functions.md
logical-order-in-if-else-statements.md
manipulate-arrays-with-pop.md
manipulate-arrays-with-push.md
manipulate-arrays-with-shift.md
manipulate-arrays-with-unshift.md
manipulating-complex-objects.md
modify-array-data-with-indexes.md
multiple-identical-options-in-switch-statements.md
multiply-two-decimals-with-javascript.md
multiply-two-numbers-with-javascript.md
nest-one-array-within-another-array.md
nesting-for-loops.md
passing-values-to-functions-with-arguments.md
practice-comparing-different-values.md
profile-lookup.md
quoting-strings-with-single-quotes.md
record-collection.md
replace-loops-using-recursion.md
replacing-if-else-chains-with-switch.md
return-a-value-from-a-function-with-return.md
return-early-pattern-for-functions.md
returning-boolean-values-from-functions.md
selecting-from-many-options-with-switch-statements.md
shopping-list.md
stand-in-line.md
store-multiple-values-in-one-variable-using-javascript-arrays.md
storing-values-with-the-assignment-operator.md
subtract-one-number-from-another-with-javascript.md
testing-objects-for-properties.md
understand-string-immutability.md
understanding-boolean-values.md
understanding-case-sensitivity-in-variables.md
understanding-undefined-value-returned-from-a-function.md
understanding-uninitialized-variables.md
updating-object-properties.md
use-bracket-notation-to-find-the-first-character-in-a-string.md
use-bracket-notation-to-find-the-last-character-in-a-string.md
use-bracket-notation-to-find-the-nth-character-in-a-string.md
use-bracket-notation-to-find-the-nth-to-last-character-in-a-string.md
use-conditional-logic-with-if-statements.md
use-multiple-conditional-ternary-operators.md
use-recursion-to-create-a-countdown.md
use-recursion-to-create-a-range-of-numbers.md
use-the-conditional-ternary-operator.md
use-the-parseint-function-with-a-radix.md
use-the-parseint-function.md
using-objects-for-lookups.md
word-blanks.md
write-reusable-javascript-with-functions.md
basic-javascript-rpg-game
debugging
es6
functional-programming
functional-programming-spreadsheet
intermediate-algorithm-scripting
intermediate-javascript-calorie-counter
javascript-algorithms-and-data-structures-projects
object-oriented-programming
regular-expressions
03-front-end-libraries
04-data-visualization
05-apis-and-microservices
06-quality-assurance
07-scientific-computing-with-python
08-data-analysis-with-python
09-information-security
10-coding-interview-prep
11-machine-learning-with-python
12-certificates
schema
test
.babelrc
LICENSE.md
comment-dictionary.js
create-challenge-bundle.js
getChallenges.acceptance.test.js
getChallenges.js
getChallenges.test.js
gulpfile.js
lib.js
package-entry.js
package-lock.json
package.json
utils.js
cypress
docs
tools
utils
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.gitpod.yml
.node-inspectorrc
.npmrc
.prettierignore
.prettierrc
.snyk
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile.tests
HoF.md
LICENSE.md
README.md
SECURITY.md
change_volumes_owner.sh
crowdin.yml
cypress-install.js
cypress.json
docker-compose-shared.yml
docker-compose.tests.yml
docker-compose.yml
jest.config.js
lerna.json
lighthouserc.js
package-lock.json
package.json
sample.env
freeCodeCamp/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/multiple-identical-options-in-switch-statements.md

122 lines
3.0 KiB
Markdown
Raw Normal View History

---
id: 56533eb9ac21ba0edf2244df
title: Multiple Identical Options in Switch Statements
challengeType: 1
videoUrl: 'https://scrimba.com/c/cdBKWCV'
forumTopicId: 18242
---
## Description
<section id='description'>
If the <code>break</code> statement is omitted from a <code>switch</code> statement's <code>case</code>, the following <code>case</code> statement(s) are executed until a <code>break</code> is encountered. If you have multiple inputs with the same output, you can represent them in a <code>switch</code> statement like this:
```js
switch(val) {
case 1:
case 2:
case 3:
result = "1, 2, or 3";
break;
case 4:
result = "4 alone";
}
```
Cases for 1, 2, and 3 will all produce the same result.
</section>
## Instructions
<section id='instructions'>
Write a switch statement to set <code>answer</code> for the following ranges:<br><code>1-3</code> - "Low"<br><code>4-6</code> - "Mid"<br><code>7-9</code> - "High"
<strong>Note</strong><br>You will need to have a <code>case</code> statement for each number in the range.
</section>
## Tests
<section id='tests'>
```yml
tests:
- text: <code>sequentialSizes(1)</code> should return "Low"
testString: assert(sequentialSizes(1) === "Low");
- text: <code>sequentialSizes(2)</code> should return "Low"
testString: assert(sequentialSizes(2) === "Low");
- text: <code>sequentialSizes(3)</code> should return "Low"
testString: assert(sequentialSizes(3) === "Low");
- text: <code>sequentialSizes(4)</code> should return "Mid"
testString: assert(sequentialSizes(4) === "Mid");
- text: <code>sequentialSizes(5)</code> should return "Mid"
testString: assert(sequentialSizes(5) === "Mid");
- text: <code>sequentialSizes(6)</code> should return "Mid"
testString: assert(sequentialSizes(6) === "Mid");
- text: <code>sequentialSizes(7)</code> should return "High"
testString: assert(sequentialSizes(7) === "High");
- text: <code>sequentialSizes(8)</code> should return "High"
testString: assert(sequentialSizes(8) === "High");
- text: <code>sequentialSizes(9)</code> should return "High"
testString: assert(sequentialSizes(9) === "High");
- text: You should not use any <code>if</code> or <code>else</code> statements
testString: assert(!/else/g.test(code) || !/if/g.test(code));
- text: You should have nine <code>case</code> statements
testString: assert(code.match(/case/g).length === 9);
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
function sequentialSizes(val) {
var answer = "";
// Only change code below this line
// Only change code above this line
return answer;
}
sequentialSizes(1);
```
</div>
</section>
## Solution
<section id='solution'>
```js
function sequentialSizes(val) {
var answer = "";
switch(val) {
case 1:
case 2:
case 3:
answer = "Low";
break;
case 4:
case 5:
case 6:
answer = "Mid";
break;
case 7:
case 8:
case 9:
answer = "High";
}
return answer;
}
```
</section>