-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex2.js
46 lines (41 loc) · 946 Bytes
/
index2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
function test(num) {
return num * 2;
}
function isOdd(num) {
return num % 2 !== 0 // true # false
}
function sum(a, b) { return a + b }
const arr = [3, 4, 5]
const arr2 = arr.filter(isOdd)
console.log(arr2)
// [a, b, c, d].reduce(+)
// reduce
// a + b + c + d
const tinhTongSoLe = arr => arr
.filter(isOdd)
.reduce(sum)
function expect (value) {
return {
toBe: (toBeValue) => {
if (toBeValue === value) {
console.log('Pass!')
} else {
throw new Error('Error!')
}
}
}
}
// test
function test (msg, func) {
try {
func()
console.log(`${msg} ket thuc kiem thu`)
} catch (err) {
console.error(`${msg} Tra lai ket qua loi`)
}
}
test('tinhTongSoLe', () => {
expect(tinhTongSoLe([1, 2, 4, 3])).toBe(4)
expect(tinhTongSoLe([1, 2, 5])).toBe(6)
expect(tinhTongSoLe([1, 2, 9, 3])).toBe(13)
})