Array.prototype.reduce = function(fn, initialValue) { let acc = initialValue; for (const [key, val] of Object.entries(this)) { if (acc === undefined && key === '0') { acc = this[0]; } else { acc = fn.call(null, acc, this[key], key, this); } } return acc; }; const arr = [3, 4, 5, 0, -1]; console.log(arr.reduce((acc, curr) => acc + curr)); // 11
Categories