Nodejs Array Sum sumItems()

Here you can find the source of sumItems()

Method Source Code

Array.prototype.sumItems = function(){
  var sum = 0;//from w  ww . jav a  2  s.  c  om
  for(var i=0; i<this.length; i++){
     sum=sum+this[i];
  }
  
  return(sum);
}
//creating arrays to test the code
var arr = new Array;
arr = [5,10];
arr.push(50);
arr.sumItems();


var myArray = [1,2,3,4,5,6,7,8,9,10];
myArray.sumItems();

Related

  1. sum(s)
    Array.prototype.sum = function (s) {
      s = s || Selector;
      var l = this.length;
      var sum = 0;
      while (l-- > 0) sum += s(this[l]);
      return sum;
    };
    
  2. sum(selector)
    Array.prototype.sum = function(selector) {
        if (typeof selector !== 'function') {
            selector = function(item) {
                return item;
        var sum = 0;
        for (var i = 0; i < this.length; i++) {
            sum += selector(this[i]);
    ...
    
  3. sum(selectorfun)
    Array.prototype.sum = function(selectorfun)
      if (selectorfun)
        return this.map(selectorfun).sum();
      else
        var ret = 0;
    ...
    
  4. sum(xAndY)
    Array.prototype.sum = function(xAndY){
      var count = 0;
      for(var i = 0; i < this.length; i++)
        count += xAndY(this[i])
      return count;
    
  5. sumArrayItems()
    Array.prototype.sumArrayItems = function() {
      var countIndex = this.length - 1;
      var sum =0;
      while ( countIndex >= 0) {
        sum = sum + this[ countIndex ];
        countIndex--;
      return sum;
    var findMissing = function( arr1 , arr2 ) {
      return Math.abs( arr1 - arr2);
    findMissing([1,2].sumArrayItems() , [1,2,5].sumArrayItems());