-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreduce2.js
35 lines (31 loc) · 1.34 KB
/
reduce2.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
Array.prototype.myReduce = function (callback, initialValue) {
const array = initialValue ? [initialValue, ...this] : this
const totalCalls = array.length - 1
const firstValue = callback(array[0], array[1])
if (totalCalls >= 1 && array[2]) {
const secondValue = callback(firstValue, array[2])
if (totalCalls >= 2 && array[3]) {
const thirdValue = callback(secondValue, array[3])
if (totalCalls >= 3 && array[4]) {
const forthValue = callback(thirdValue, array[4])
if (totalCalls >= 4 && array[5]) {
// known / deliberate limitation of this approach -
// only works on an array of max 5 values
throw new Error('No, this reduce only works for 5 element arrays')
} else {
return forthValue
}
} else {
return thirdValue
}
} else {
return secondValue
}
} else {
return firstValue
}
}
const withInitialValue = [3, 2, 1, 4].myReduce((accumulator, currentValue) => { return accumulator + currentValue }, 5)
console.log(withInitialValue)
const missingInitialValue = [3, 2, 1, 4].myReduce((accumulator, currentValue) => { return accumulator + currentValue })
console.log(missingInitialValue)