forked from codediodeio/code-this-not-that-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloops.js
62 lines (42 loc) · 900 Bytes
/
loops.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const orders = [500, 30, 99, 15, 223];
'Bad Loop Code 💩'
const total = 0;
const withTax = [];
const highValue = [];
for (i = 0; i < orders.length; i++) {
// Reduce
total += orders[i];
// Map
withTax.push(orders[i] * 1.1);
// Filter
if (orders[i] > 100) {
highValue.push(orders[i])
}
}
'Good Loop Code ✅'
// Reduce
const total = orders.reduce((acc, cur) => acc + cur)
// Map
const withTax = orders.map(v => v * 1.1)
// Filter
const highValue = orders.filter(v => v > 100);
/**
* Every
* @returns false
*/
const everyValueGreaterThan50 = orders.every(v => v > 50)
/**
* Every
* @returns true
*/
const everyValueGreaterThan10 = orders.every(v => v > 10)
/**
* Some
* @returns false
*/
const someValueGreaterThan500 = orders.some(v => v > 500)
/**
* Some
* @returns true
*/
const someValueGreaterThan10 = orders.some(v => v > 10)