Nodejs Array Sum sum()

Here you can find the source of sum()

Method Source Code

var ary1 = new Array(12, 55, 43, 32, 44);
var ary2 = new Array(12, 33, 1);
Array.prototype.sum = function() {
    var output = 0;
    for (var i = 0; i < this.length; i++) {
        output += this[i];//from  w  w w.  j  a  v a  2  s. co m
    }
    return output;
};
console.log(ary1.sum());
console.log(ary2.sum());

Related

  1. sum()
    var numbers = [1,2,3,4,5,6];
    Array.prototype.sum = function(){
      return this.reduce(function(p, c){
        return p+c;
      })
    var result = numbers.sum();
    console.log(result);
    console.log(numbers);
    ...
    
  2. sum()
    Array.prototype.sum = function(){
      for(var i=0,sum=0;i<this.length;sum+=this[i++]);
      return sum;
    Array.prototype.max = function(){
      return Math.max.apply({},this)
    Array.prototype.min = function(){
      return Math.min.apply({},this)
    ...
    
  3. sum()
    Array.prototype.sum = function() {
      var i;
      var s:Number = 0;
      for (i=0; i < this.length; i++) {
        s += Number(this[i]);
      return s;
    var test = new Array(1, 2, 3);
    ...
    
  4. sum()
    'use strict';
    Array.prototype.sum = function () {
        var total = 0;
        var i = this.length;
        while (i--) {
            total += this[i];
        return total;
    };
    ...
    
  5. sum()
    Array.prototype.sum = function() {
      total = 0;
      for (var i=0;i < this.length;i++) {
        total += this[i];
      return total;
    };
      var a = [];
      for (var i=0;i < 1000;i++) {
    ...
    
  6. sum()
    Array.prototype.sum = function(){
      var sum = 0;
      for(var index in this){
        if(typeof this[index] === "number"){
          sum += this[index];
      return sum;
    var numbers = [1,2,3,4,5];
    console.log(numbers.sum());
    
  7. sum()
    Array.prototype.sum = function(){
      var sum = 0;
      for(var i = 0; i < this.length; i++){
        if(typeof this[i] === "number"){
          sum += this[i];
      return sum;
    var numbers = [1,2,3,4,5];
    console.log(numbers.sum());
    
  8. sum()
    var ary1 = new Array(12, 55, 43, 32, 44);
    var ary2 = new Array(12, 33, 1);
    Array.prototype.sum = function() {
        var output = 0;
        for (var i = 0; i < this.length; i++) {
            output += this[i];
        return output;
    };
    ...
    
  9. sum()
    Array.prototype.sum = function(){
      for(var i=0,sum=0;i<this.length;sum+=this[i++]);
      return sum;
    Array.prototype.max = function(){
      return Math.max.apply({},this)
    Array.prototype.first = function(){
      return this[0];
    ...