-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreduce.js
More file actions
39 lines (36 loc) · 996 Bytes
/
Copy pathreduce.js
File metadata and controls
39 lines (36 loc) · 996 Bytes
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
function fold(arr, func, accumulator = 0) {
for (let i = 0; i < arr.length; i++) {
accumulator = (func(accumulator, arr[i]))
}
return accumulator
}
function foldRight(arr, func, accumulator = 0) {
for (let i = arr.length - 1; i >= 0; i--) {
console.log(arr[i])
accumulator = (func(accumulator, arr[i]))
}
return accumulator
}
function reduce(arr, func, accumulator) {
if (typeof arr[0] === 'number') {
accumulator = 0
} else {
accumulator = ''
}
for (let i = 0; i < arr.length; i++) {
accumulator = (func(accumulator, arr[i]))
}
return accumulator
}
function reduceRight(arr, func, accumulator) {
if (typeof arr[0] === 'number') {
accumulator = 0
} else {
accumulator = ''
}
for (let i = arr.length - 1; i >= 0; i--) {
console.log(arr[i])
accumulator = (func(accumulator, arr[i]))
}
return accumulator
}