Javascript Array sumItems()

Description

Javascript Array sumItems()


Array.prototype.sumItems = function(){
  var sum = 0;//w w  w .  j  av 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();



PreviousNext

Related