Nodejs Array Sum sum()

Here you can find the source of sum()

Method Source Code

Array.prototype.sum /* -> sum */ = function() {
    var res = 0;/* w w  w  .  j a  v a 2s. c o  m*/
    for(var p in this)
   res += this[p];
    return res;
};

var a = [1, 2, 3];
console.log(0 in a);

Related

  1. sum()
    Array.prototype.sum = function () {
      let s = 0;
      for (let i = 0; i < this.length; i++) {
        s += this[i];
      return s;
    };
    
  2. sum()
    Array.prototype.sum = function() {
      var sum = 0;
      for (var i = 0, l = this.length; i < l; i++) {
        sum += this[i];
      return sum;
    };
    
  3. sum()
    Array.prototype.sum = function () {
        var sum = 0;
        for (i = 0; i < this.length; i++) {
          sum += this[i]
       return sum 
    
  4. sum()
    Array.prototype.sum = function() {
        var res = 0;
        for(var p in this)
      res += this[p];
        return res;
    };
    var s = [1, 2, 3].sum();
    
  5. sum()
    Array.prototype.sum = function() {
      return this.reduce(function(prev, curr) {
        return prev + curr;
      });
    };
    module.exports = Array.prototype.sum;
    
  6. sum()
    Array.prototype.sum = function() {
        var res = 0;
        for(var i=0;i<this.length;++i)
      res += this[i];
        return res;
    };
    var s = [1, 2, 3].sum();
    
  7. sum()
    Array.prototype.sum = function () {
      var sum = 0;
      for (var i = 0; i < this.length; i++) {
        sum += this[i];
      };
      return sum;
    console.log([1, 3, 12, 33, 2].sum());
    
  8. sum()
    Array.prototype.sum = function () {
        var length = this.length;
        var total = 0;
        for (var i = 0; i < length; i++) {
           total += this[i];
        return total;
    };
    
  9. sum()
    const assert = require('assert')
    Array.prototype.sum = function() {
      return this.reduce((sum, el) => sum + el, 0)
    assert.equal([].sum(), 0)
    assert.equal([2, 5, 7].sum(), 14)