Skip to content

Commit cde12d9

Browse files
committed
Section 32: Using Mocha darle play
1 parent b975ddd commit cde12d9

5 files changed

Lines changed: 1455 additions & 0 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules
2+
node_modules/
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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const assert = require('assert')
2+
const { forEach, map } = require('./index')
3+
4+
it('The forEach function', () => {
5+
let sum = 0
6+
forEach([1, 2, 3], (value) => {
7+
sum += value
8+
})
9+
10+
// Optionally we can pass a third argument of an error message
11+
assert.strictEqual(sum, 6)
12+
// assert.strictEqual(sum, 7, 'Expected forEach to sum the array')
13+
})
14+
15+
it('The map funciton', () => {
16+
const result = map([1, 2, 3], (value) => {
17+
return value * 2
18+
})
19+
20+
// assert.deepStrictEqual(result, [2, 4, 7]) // this throws an error
21+
assert.deepStrictEqual(result, [2, 4, 6])
22+
})

0 commit comments

Comments
 (0)