Skip to content

Commit fa7f972

Browse files
committed
feat: add control flow exercises with solutions and tests
- Introduced new exercises on loops and nested loops, including a seating chart problem. - Updated existing loop exercise to build a numbered list of exhibits. - Added ternary operator exercise for concise value selection. - Created corresponding solution and test files for all new exercises. - Updated package-lock.json to reflect new dependencies.
1 parent c56b535 commit fa7f972

20 files changed

Lines changed: 178 additions & 78 deletions

File tree

exercises/04.control-flow/03.problem.loops/README.mdx

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,25 @@
11
# Loops
22

3-
👨‍💼 Let's build a fun text banner using loops. We'll make a simple "staircase"
4-
pattern of `*` characters.
3+
👨‍💼 Let's practice a simple `for` loop by building a numbered list.
54

6-
🐨 Open <InlineFile file="index.ts" /> and:
5+
🐨 Open <InlineFile file="index.ts" /> and use a `for` loop to build a string
6+
with labels for exhibits 1 through 5. Each label should be on its own line.
77

8-
1. Use a `for` loop to build 5 rows of stars
9-
2. Each row should have one more `*` than the previous row
10-
3. Put each row on its own line using `\n`
11-
12-
💰 Traditional `for` loop structure:
8+
## How a `for` loop works
139

1410
```ts
15-
for (let i = 0; i < count; i++) {
16-
// do something
11+
for (let i = 0; i < limit; i++) {
12+
// repeat work
1713
}
1814
```
1915

20-
💰 String building with a loop:
21-
22-
```ts
23-
let output = ''
24-
for (...) {
25-
output += 'x'
26-
}
27-
```
16+
Each part of the loop has a job:
2817

29-
💰 You'll need **two loops** for this exercise—an outer loop for each row, and
30-
an inner loop to build the stars for that row. This is called a "nested loop":
18+
- **Initializer** (`let i = 0`) runs once before the loop starts
19+
- **Condition** (`i < limit`) is checked before each iteration
20+
- **Final expression** (`i++`) runs after each iteration
3121

32-
```ts
33-
for (let row = 1; row <= 5; row++) {
34-
// inner loop builds stars for this row
35-
for (let star = 0; star < row; star++) {
36-
// add a star
37-
}
38-
}
39-
```
22+
`i++` means "add 1 to `i`." It's the same as `i += 1`.
4023

4124
📜 [MDN - for statement](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for)
4225

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
// Star Staircase
2-
// Using loops to build a fun string pattern
1+
// Exhibit Labels
2+
// Using a for loop to build a numbered list
33

4-
// 🐨 Create a variable `output` starting as an empty string
5-
// 💰 let output = ''
4+
// 🐨 Create a variable `exhibitLabels` starting as an empty string
5+
// 💰 let exhibitLabels = ''
66

7-
// 🐨 Write a for loop that builds 5 rows of stars
8-
// Each row should be one star longer than the previous
9-
// 💰 for (let row = 1; row <= 5; row++)
7+
// 🐨 Write a for loop that counts from 1 to 5
8+
// 🐨 On each pass, add a line like "Exhibit 1" to the string
9+
// 🐨 Put each label on its own line using "\n"
1010

11-
// Inside the loop:
12-
// 🐨 Build a single line of stars for this row
13-
// 🐨 Add that line plus a "\n" to the output
14-
15-
// console.log(output)
11+
// console.log(exhibitLabels)
1612

1713
// 🐨 Export your variable so we can verify your work
18-
// export { output }
14+
// export { exhibitLabels }
Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
# Loops
22

3-
👨‍💼 Great job! You used loops to build a string step by step—a very common
4-
pattern when generating output.
3+
👨‍💼 Nice work! You used a simple `for` loop to build a string step by step.
54

6-
🦉 The string-building pattern:
7-
8-
1. Start with an empty string
9-
2. Build one piece at a time
10-
3. Add separators like `\n` between pieces
11-
12-
This pattern shows up when generating reports, formatting text, or building
13-
simple ASCII art.
5+
🦉 The loop made it easy to repeat a small action (adding one label) without
6+
copying and pasting code. This is the heart of loops: repeating a small unit of
7+
work while keeping track of progress with a counter.

exercises/04.control-flow/03.solution.loops/index.test.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@ import assert from 'node:assert/strict'
22
import { test } from 'node:test'
33
import * as solution from './index.ts'
44

5-
await test('output is exported', () => {
5+
await test('exhibitLabels is exported', () => {
66
assert.ok(
7-
'output' in solution,
8-
'🚨 Make sure you export "output" - add: export { output }',
7+
'exhibitLabels' in solution,
8+
'🚨 Make sure you export "exhibitLabels" - add: export { exhibitLabels }',
99
)
1010
})
1111

12-
await test('output should match the star staircase', () => {
13-
const expected = '*\n**\n***\n****\n*****\n'
12+
await test('exhibitLabels should list exhibits 1 through 5', () => {
13+
const expected =
14+
'Exhibit 1\nExhibit 2\nExhibit 3\nExhibit 4\nExhibit 5\n'
1415
assert.strictEqual(
15-
solution.output,
16+
solution.exhibitLabels,
1617
expected,
17-
'🚨 output should be a 5-line staircase of stars (each row has one more star than the previous)',
18+
'🚨 exhibitLabels should list Exhibit 1 through Exhibit 5 on separate lines',
1819
)
1920
})
Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
// Star Staircase
2-
// Using loops to build a fun string pattern
1+
// Exhibit Labels
2+
// Using a for loop to build a numbered list
33

4-
let output = ''
4+
let exhibitLabels = ''
55

6-
for (let row = 1; row <= 5; row++) {
7-
let line = ''
8-
for (let count = 0; count < row; count++) {
9-
line += '*'
10-
}
11-
12-
output += `${line}\n`
6+
for (let exhibitNumber = 1; exhibitNumber <= 5; exhibitNumber++) {
7+
exhibitLabels += `Exhibit ${exhibitNumber}\n`
138
}
149

15-
console.log(output)
10+
console.log(exhibitLabels)
1611

17-
export { output }
12+
export { exhibitLabels }
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Nested Loops
2+
3+
👨‍💼 Let's generate a seating chart for a small theater. Each row has multiple
4+
seats, so we'll need a loop inside a loop.
5+
6+
🐨 Open <InlineFile file="index.ts" /> and build a string that looks like this:
7+
8+
```
9+
A1 A2 A3 A4
10+
B1 B2 B3 B4
11+
C1 C2 C3 C4
12+
```
13+
14+
Each row should be on its own line using `\n`.
15+
16+
Use **nested `for` loops**:
17+
18+
- The **outer loop** controls which row you're on
19+
- The **inner loop** builds the seat labels for that row
20+
21+
📜 [MDN - for statement](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Theater Seating Chart
2+
// Use nested loops to build rows and seats
3+
4+
const rows = ['A', 'B', 'C']
5+
const seatsPerRow = 4
6+
7+
// 🐨 Create a variable `seatChart` starting as an empty string
8+
// 💰 let seatChart = ''
9+
10+
// 🐨 Use nested for loops to build the seating chart
11+
// 🐨 Each row should include seat labels like "A1 A2 A3 A4"
12+
// 🐨 Put each row on its own line using "\n"
13+
14+
// console.log(seatChart)
15+
16+
// 🐨 Export your variable so we can verify your work
17+
// export { seatChart }
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "exercises_04.control-flow_04.problem.nested-loops",
3+
"type": "module",
4+
"scripts": {
5+
"start": "npx @kentcdodds/log-module ./index.ts",
6+
"start:watch": "npx @kentcdodds/log-module --watch ./index.ts",
7+
"test": "node --test index.test.ts",
8+
"test:watch": "node --watch --test index.test.ts"
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Nested Loops
2+
3+
👨‍💼 Great work! Nested loops are perfect when you need to build a grid, table,
4+
or other row/column structure.
5+
6+
🦉 A helpful pattern is:
7+
8+
1. Use the **outer loop** to pick the row
9+
2. Build a **single row string**
10+
3. Add that row plus `\n` to the full output
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 * as solution from './index.ts'
4+
5+
await test('seatChart is exported', () => {
6+
assert.ok(
7+
'seatChart' in solution,
8+
'🚨 Make sure you export "seatChart" - add: export { seatChart }',
9+
)
10+
})
11+
12+
await test('seatChart should match the seating chart', () => {
13+
const expected = 'A1 A2 A3 A4\nB1 B2 B3 B4\nC1 C2 C3 C4\n'
14+
assert.strictEqual(
15+
solution.seatChart,
16+
expected,
17+
'🚨 seatChart should list rows A-C with seats 1-4 on separate lines',
18+
)
19+
})

0 commit comments

Comments
 (0)