Nodejs Array Sum sum()

Here you can find the source of sum()

Method Source Code

Array.prototype.sum = function () {
    var total = 0;
    var i = this.length;

    while (i--) {
        total += this[i];/* w w w .  ja  v a  2s  .  co m*/
    }

    return total;
}

function isEmpty(obj) {
    if(obj === undefined || obj === null){
        return true
    }
    if(Array.isArray(obj)) {
        return obj.length === 0
    }
    else{
        return Object.keys(obj).length === 0;
    }
}

Related

  1. sum()
    Array.prototype.sum = function () {
      return this.reduce(function(sum, val){ 
        return sum + val
      })
    console.log([1, 2, 3, 4].sum());
    
  2. sum()
    Array.prototype.sum = function() {
      return this.reduce((result, element) => result + element, 0);
    };
    
  3. sum()
    function range(a, b) {
        return [...Array(b-a).keys()].map(x => x + a);
    Array.prototype.sum = function() {
        return this.reduce((a, b) => a + b, 0);
    module.exports = function(n) {
        var sumOfSquares = range(1,101).map(x => x*x).sum();
        var squareOfSum = Math.pow(range(1,101).sum(), 2);
    ...
    
  4. sum()
    Array.prototype.sum = function() {
      return this.reduce(function(p, c, i, arr) {
        return p + c;
      });
    };
    var a = [];
    process.stdin.resume();
    process.stdin.setEncoding("utf8");
    process.stdin.on("data", getNum);
    ...
    
  5. sum()
    Array.prototype.sum = function(){
      var val = 0;
      this.forEach(function(v){
        val +=v;
      });
      return val;
    };
    
  6. sum()
    Array.prototype.sum = function () {
      return this.reduce((sum, n) => sum + Number(n), 0)
    
  7. sum()
    Array.prototype.sum = function () {
      return this.reduce(function (total, aValue) {
        return total + Number(aValue);
      });
    };
    
  8. sum()
    var log = console.log.bind(console)
    Array.prototype.sum = function() {
        return this.reduce(function(previous, current) {
            return previous + current
        })
    var numbers = [1, 2, 3, 4, 5, 6]
    var result = numbers.sum()
    log(result)
    ...
    
  9. sum()
    Array.prototype.sum=function(){
       return this.reduce(function(s,n){return s+n})