Skip to content

Commit 03175a9

Browse files
committed
Section 32: Fixing Three Issues
1 parent 365641c commit 03175a9

3 files changed

Lines changed: 58 additions & 0 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module.exports = {
2+
forEach(arr, fn) {
3+
for (let index in arr) {
4+
fn(arr[index], index)
5+
}
6+
},
7+
map(arr, fn) {
8+
const result = []
9+
for (let i = 0; i < arr.length; i++) {
10+
result.push(fn(arr[i], i))
11+
}
12+
13+
return result
14+
},
15+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const { forEach, map } = require('./index')
2+
3+
const test = (desc, fn) => {
4+
console.log('----', desc)
5+
try {
6+
fn()
7+
} catch (err) {
8+
console.log(err.message)
9+
}
10+
}
11+
12+
test('The forEach function', () => {
13+
let sum = 0
14+
forEach([1, 2, 3], (value) => {
15+
sum += value
16+
})
17+
18+
// if (sum !== 7) { // This throws an error
19+
if (sum !== 6) {
20+
throw new Error('Expected summing array to equal 6')
21+
}
22+
})
23+
24+
test('The map funciton', () => {
25+
const result = map([1, 2, 3], (value) => {
26+
return value * 2
27+
})
28+
// result === [2, 4, 6]
29+
30+
if (result[0] !== 2) {
31+
throw new Error(`Expected to find 2, but found ${result[0]}`)
32+
}
33+
34+
if (result[1] !== 4) {
35+
throw new Error(`Expected to find 4, but found ${result[1]}`)
36+
}
37+
38+
if (result[2] !== 6) {
39+
throw new Error(`Expected to find 6, but found ${result[2]}`)
40+
}
41+
})

32-The-Basics-of-Testing/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@
2121
## 3. 03 A No-Frills Testing Implementation
2222

2323
## 4. 04 Test Driven Development
24+
25+
## 5. 05 Fixing Three Issues

0 commit comments

Comments
 (0)