Nodejs Array Reduce reduce(process, initial)

Here you can find the source of reduce(process, initial)

Method Source Code

/*//from   w  w  w  . j a va  2s. c  om

Description:

In this kata, you must define the Array.reduce method.

I have disabled the pre-existing reduce methods.

Here's how it works:

[1,2,3].reduce( function(sum, next){return sum+next}, 0) 
// => 6

['a','b','a'].reduce( function(obj, elem){if(!obj[elem]) obj[elem] = 0; obj[elem] += 1; return obj}, {})
// => {'a':2, 'b':1}
Summary: The reduce method goes through each element of an array, applies the function to whatever the function returned last, and returns the last outcome. On the first iteration, it should pass the initial value to the function, as well as the first element of the array. If no initial value is passed, skip to the first element of the array.

Ruby methods should expect a lambda.

*/

Array.prototype.reduce = function(process, initial) {
  if (initial === undefined) {
    var slice = this.slice(1);
    initial = this[0];
  }
  var array = slice || this;
  array.forEach(function(value) {
    initial = process(initial, value);
  });
  return initial;
};

Related

  1. reduce(fun /* , initial */)
    if (!Array.prototype.reduce) {
      Array.prototype.reduce = function(fun ) {
        var len = this.length >>> 0;
        if (typeof fun != "function")
          throw new TypeError();
        if (len == 0 && arguments.length == 1)
          throw new TypeError();
        var i = 0;
        if (arguments.length >= 2) {
    ...
    
  2. reduce(fun /*, initial*/)
    Array.prototype.reduce = function(fun )
      var len = this.length;
      if (typeof fun != "function") {
        throw new TypeError();
      if (len == 0 && arguments.length == 1) {
        throw new TypeError();
      var i = 0;
      if (arguments.length >= 2) {
        var rv = arguments[1];
      } else {
        do {
          if (i in this) {
            rv = this[i++];
            break;
          if (++i >= len) {
            throw new TypeError();
        } while (true);
      for (; i < len; i++) {
        if (i in this) {
          rv = fun.call(null, rv, this[i], i, this);
      return rv;
    };
    
  3. reduce(fun /*, initial*/)
    Array.prototype.reduce = Array.prototype.reduce || function(fun ) {
        var len = this.length >>> 0;
        if (typeof fun != "function") {
            throw new TypeError();
        if (len == 0 && arguments.length == 1) {
            throw new TypeError();
        var i = 0;
    ...
    
  4. reduce(pasteback, initial)
    'use strict';
    Array.prototype.reduce = function(pasteback, initial){
      for(var i = 0
          , c =  this.length
          , value = arguments.length>1 ? initial : this[i++]
      ; i<c; i++){
        value = pasteback(value, this[i], i, this);
      return value;
    ...
    
  5. reduce(process, initial)
    Array.prototype.reduce = function(process, initial) {
      var value = initial;
      for (var i = 0; i < this.length; i++) {
        if( ( i === 0 ) && ( value === undefined || value === null ))
            value = this[0];
            continue;
        value = process( value, this[i] );
    ...
    
  6. reduce(reducingFunction, initialValue)
    Array.prototype.reduce = function(reducingFunction, initialValue) {
      var result = initialValue;
      this.forEach(function(x) {
        reducingFunction(result, x)
      });
      return result;
    
  7. reduce(t, fn)
    Array.prototype.reduce = function(t, fn) {
      for(var i = 0 ; i < this.length ; i++) {
        t = fn(t, this[i])
      return t
    
  8. reduce(testFunction,initialValue)
    Array.prototype.reduce = function(testFunction,initialValue){
      var accumulatedValue,counter;
      if(this.length == 0){
        return this;
      }else{
        if(arguments.length == 1){
          counter = 1;
          accumulatedValue = this[0];
        }else if(arguments.length >= 2){
    ...
    
  9. reduce(testFunction,initialValue)
    Array.prototype.reduce = function(testFunction,initialValue){
      var accumulatedValue,counter;
      if(this.length == 0){
        return this;
      }else{
        if(arguments.length == 1){
          counter = 1;
          accumulatedValue = this[0];
        }else if(arguments.length >= 2){
    ...