-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreduce.js
More file actions
19 lines (16 loc) · 944 Bytes
/
Copy pathreduce.js
File metadata and controls
19 lines (16 loc) · 944 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* callback - це функція зворотного виклику, яка буде викликатися для кожного
елемента масиву. Функція зворотного виклику приймає чотири аргументи: акумулятор,
поточний елемент, індекс поточного елемента і посилання на вихідний масив. */
Array.prototype.customReduce = function (callback, initialValue) {
let accumulator = initialValue !== undefined ? initialValue : this[0];
let startIndex = initialValue !== undefined ? 0 : 1;
for (let i = startIndex; i < this.length; i++) {
accumulator = callback(accumulator, this[i], i, this);
}
return accumulator;
};
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.customReduce(function (accumulator, currentValue) {
return accumulator + currentValue;
});
console.log(sum);