Skip to content

Commit ed5763e

Browse files
committed
add switch statements exercise to control flow
Introduces a new step (02.switch-statements) that teaches switch statements by converting letter grades to descriptions, building on the conditionals exercise theme. Renumbers loops to 03 and for-of-iteration to 04.
1 parent 529e8d9 commit ed5763e

File tree

23 files changed

+178
-4
lines changed

23 files changed

+178
-4
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Switch Statements
2+
3+
👨‍💼 We've been using `if`/`else if` chains, but when you're checking one value
4+
against many possible matches, a `switch` statement is often cleaner.
5+
6+
🐨 Open <InlineFile file="index.ts" /> and:
7+
8+
1. Create a `description` variable to hold the feedback text
9+
2. Write a `switch` statement that checks the `grade` value and sets the
10+
appropriate description:
11+
- 'A' → "Excellent"
12+
- 'B' → "Good"
13+
- 'C' → "Satisfactory"
14+
- 'D' → "Needs Improvement"
15+
- 'F' → "Failing"
16+
- Any other value → "Invalid grade"
17+
18+
💰 Switch statement structure:
19+
20+
```ts
21+
switch (value) {
22+
case 'option1':
23+
// do something
24+
break
25+
case 'option2':
26+
// do something else
27+
break
28+
default:
29+
// handle all other cases
30+
}
31+
```
32+
33+
<callout-warning>
34+
Don't forget the `break` statement after each case! Without it, execution
35+
"falls through" to the next case.
36+
</callout-warning>
37+
38+
📜 [MDN - switch statement](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch)
39+
40+
🐨 Once you've completed the exercise, run `node index.ts` in the playground to
41+
test your work!
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Grade Description
2+
// Convert letter grades to descriptive feedback using switch
3+
4+
const grade: string = 'B'
5+
6+
// 🐨 Create a variable `description` of type string
7+
// 💰 let description: string
8+
9+
// 🐨 Write a switch statement on `grade`:
10+
// - case 'A': set description to "Excellent"
11+
// - case 'B': set description to "Good"
12+
// - case 'C': set description to "Satisfactory"
13+
// - case 'D': set description to "Needs Improvement"
14+
// - case 'F': set description to "Failing"
15+
// - default: set description to "Invalid grade"
16+
// 💰 Don't forget the `break` after each case!
17+
18+
// console.log(`Grade ${grade}: ${description}`)
19+
20+
// 🦺 This line ensures TypeScript treats this as a module
21+
export {}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "exercises_04.control-flow_02.problem.switch-statements",
3+
"type": "module",
4+
"scripts": {
5+
"start": "node index.ts",
6+
"test": "node --test index.test.ts"
7+
}
8+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Switch Statements
2+
3+
👨‍💼 Nice! The `switch` statement makes it clear we're matching one value against
4+
several options.
5+
6+
🦉 When to use `switch` vs `if`/`else`:
7+
8+
- **Use `switch`** when comparing one value against multiple specific values
9+
- **Use `if`/`else`** when checking different conditions or ranges
10+
11+
```ts
12+
// ✅ Good use of switch - matching exact values
13+
switch (grade) {
14+
case 'A':
15+
return 'Excellent'
16+
// ...
17+
}
18+
19+
// ✅ Good use of if/else - checking ranges
20+
if (score >= 90) {
21+
grade = 'A'
22+
} else if (score >= 80) {
23+
grade = 'B'
24+
}
25+
```
26+
27+
The `switch` statement also supports "fall-through" where multiple cases can
28+
share the same code—but that's an advanced pattern we won't cover here.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import assert from 'node:assert/strict'
2+
import { test } from 'node:test'
3+
import { grade, description } from './index.ts'
4+
5+
await test('grade should be "B"', () => {
6+
assert.strictEqual(
7+
grade,
8+
'B',
9+
'🚨 grade should be "B" - check your variable assignment',
10+
)
11+
})
12+
13+
await test('description should be "Good" for grade "B"', () => {
14+
assert.strictEqual(
15+
description,
16+
'Good',
17+
'🚨 description should be "Good" for grade "B" - use a switch statement to match grade to description',
18+
)
19+
})
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Grade Description
2+
// Convert letter grades to descriptive feedback using switch
3+
4+
const grade: string = 'B'
5+
6+
let description: string
7+
8+
switch (grade) {
9+
case 'A':
10+
description = 'Excellent'
11+
break
12+
case 'B':
13+
description = 'Good'
14+
break
15+
case 'C':
16+
description = 'Satisfactory'
17+
break
18+
case 'D':
19+
description = 'Needs Improvement'
20+
break
21+
case 'F':
22+
description = 'Failing'
23+
break
24+
default:
25+
description = 'Invalid grade'
26+
}
27+
28+
console.log(`Grade ${grade}: ${description}`)
29+
30+
export { grade, description }
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "exercises_04.control-flow_02.solution.switch-statements",
3+
"type": "module",
4+
"scripts": {
5+
"start": "node index.ts",
6+
"test": "node --test index.test.ts"
7+
}
8+
}
File renamed without changes.
File renamed without changes.

exercises/04.control-flow/02.problem.loops/package.json renamed to exercises/04.control-flow/03.problem.loops/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "exercises_04.control-flow_02.problem.loops",
2+
"name": "exercises_04.control-flow_03.problem.loops",
33
"type": "module",
44
"scripts": {
55
"start": "node index.ts",

0 commit comments

Comments
 (0)