Nodejs Array Sum sum( iterator )

Here you can find the source of sum( iterator )

Method Source Code

/**/*from   w  ww. j  ava2 s.c  o  m*/
* Paste bin for general JavaScript language extensions to be compiled into the production
* and lexically scoped out via an anonomous function.
*/


// sum fn extension to the Array object can recursive sum up array values
Array.prototype.sum = function ( iterator ) {
   "use strict";
   var i = ( typeof iterator === "undefined" ) ?  0 : iterator,
      total = 0;

   // check for nested array structure
   if ( this[ i ] instanceof Array ) {
      for ( i; i < this.length; i++) {
         // now sum rows by recursing into this fn
         total += this[i].sum();
      }
   }
   else {
   // not a nested structure
      for (i = 0; i < this.length; i++) {
         total += this[i];
      }
   }

   return total;
};

Related

  1. sum()
    Array.prototype.sum = function() {
      return this.reduce(function(a,b){return a+b;});
    };
    
  2. sum()
    Array.prototype.sum=function(){
      var suma;
      for(var i=0;i<this.lenght;i++){
        suma=suma+this[i];
      return suma;
    
  3. sum()
    Array.prototype.sum = function () {
      let s = 0;
      for (let i = 0; i < this.length; i++) {
        s += this[i];
      return s;
    };
    
  4. sum()
    Array.prototype.sum = function() {
      var sum = 0;
      for (var i = 0, l = this.length; i < l; i++) {
        sum += this[i];
      return sum;
    };