You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
var reduce = Function.call.bind(Array.prototype.reduce);
// actually applying some thought -- this function differs slightly from borrowing Array's prototype by switching the placement of the fn and collection parameters
var reduce = function(fn, collection, startingAcc) {
let acc = undefined;
let j = 0;
if(startingAcc) {
acc = startingAcc;
} else {
acc = collection[0];
j = 1;
}
for(null; j < collection.length; j++) {
acc = fn(acc, collection[j]);
}
return acc;
};
// we'll test with a simple sum example
let sum = (acc, x) => x + acc;
reduce(sum, [1,2,3]); // 6
// let's use reduce to create our own map function