Here you can find the source of reduce(combiner, initialValue)
// Exercise 16: Implement reduce() // Let's add a reduce() function to the Array type. Like map // [1,2,3].reduce(function(accumulatedValue, currentValue) { return accumulatedValue + currentValue; }); === [6]; // [1,2,3].reduce(function(accumulatedValue, currentValue) { return accumulatedValue + currentValue; }, 10); === [16]; Array.prototype.reduce = function (combiner, initialValue) { var counter,/*from w ww. jav a 2s .c o m*/ accumulatedValue; // If the array is empty, do nothing if (this.length === 0) { return this; } // If the user didn't pass an initial value, use the first item. if (arguments.length === 1) { counter = 1; accumulatedValue = this[0]; } else if (arguments.length >= 2) { counter = 0; accumulatedValue = initialValue; } else { throw "Invalid arguments."; } // Loop through the array, feeding the current value and the result of // the previous computation back into the combiner function until // we've exhausted the entire array and are left with only one value. while (counter < this.length) { accumulatedValue = combiner(accumulatedValue, this[counter]); counter++; } return [accumulatedValue]; };
Array.prototype.reduce = function (callback, memo) { var obj = this; var value, accumulated_value = 0; for (var i = 0; i < obj.length; i++) { value = obj[i]; accumulated_value = callback.call(null, accumulated_value, value); return accumulated_value; }; ...
Array.prototype.reduce = function(cb, res){ var arr = Object(this); for(var i=0;i<arr.length;i++){ res = cb.call(res, arr[i], i); return res;
Array.prototype.reduce = function (combiner, initialValue) { var counter, accumulatedValue; if (this.length === 0) { return this; else { if (arguments.length === 1) { counter = 1; ...
Array.prototype.reduce = function(combiner, initialValue) { var counter, accValue; if (this.length === 0) { return this; else { if (arguments.length === 1) { counter = 1; ...
Array.prototype.reduce = function(combiner, initialValue) { var counter, accumulatedValue; if (this.length === 0) { return this; else { if (arguments.length === 1) { counter = 1; ...
Array.prototype.reduce = function(combiner, initialValue) { var counter, accumulatedValue; if (this.length === 1) { return this; } else { if (arguments.length === 1) { counter = 1; accumulatedValue = this[0]; ...
Array.prototype.reduce = function(f, firstValue){ var value = firstValue; for (var i =0; i<this.length; i++){ value = f(this[i], value); return value; }; [1,2,3].reduce(function(currentValue ,value){return value+=currentValue;}, 0); [1,2,3].reduce(function(currentValue ,value){return value*=currentValue;}, 0); ...
var arr = [0,1,2,3,4]; var sum = function(x, y) { return x + y; }; Array.prototype.reduce = function(f, inital) { var acc = initial; for (var i = 0; i < this.length; i++) { acc = f(acc, this[i]); return acc; }; ...
var arr = [0,1,2,3,4]; var sum = function(total, newValue) { return total + newValue; }; Array.prototype.reduce = function(f, initial) { var acc = initial; for (var i = 0; i < this.length; i++) { acc = f(acc, this[i]); return acc; }; ...