Nodejs Array Sum sum()

Here you can find the source of sum()

Method Source Code

var log = console.log.bind(console)

Array.prototype.sum = function() {
    return this.reduce(function(previous, current) {
        return previous + current
    })//from   w w w .ja  v a 2s  . c  om
}

var numbers = [1, 2, 3, 4, 5, 6]
var result = numbers.sum()
log(result)

String.prototype.capitalize = function() {
    // log('in')
    return this.charAt(0).toUpperCase() + this.substring(1)
}

var message = 'hello world!'
log(message.capitalize())

Related

  1. 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);
    ...
    
  2. sum()
    Array.prototype.sum = function(){
      var val = 0;
      this.forEach(function(v){
        val +=v;
      });
      return val;
    };
    
  3. sum()
    Array.prototype.sum = function () {
        var total = 0;
        var i = this.length;
        while (i--) {
            total += this[i];
        return total;
    function isEmpty(obj) {
    ...
    
  4. sum()
    Array.prototype.sum = function () {
      return this.reduce((sum, n) => sum + Number(n), 0)
    
  5. sum()
    Array.prototype.sum = function () {
      return this.reduce(function (total, aValue) {
        return total + Number(aValue);
      });
    };
    
  6. sum()
    Array.prototype.sum=function(){
       return this.reduce(function(s,n){return s+n})
    
  7. sum()
    Array.prototype.sum = function()
        var sum = 0;
        for(var i = this.length; -- i >= 0 ;)
            sum += (this[i] === null || this[i] === undefined) ? 0 : this[i];
        return sum;
    
  8. sum()
    Array.prototype.sum = function() {
      for(var sum = i = 0; i < this.length; i++) {
        sum += this[i]
      return sum
    var arr = [1,21,3,4,22,45,6,7,32];
    console.log(arr.join("+")+"="+arr.sum());
    
  9. sum()
    Array.prototype.sum = function() {
      var i, sum;
      sum = 0;
      for (i=0; i<this.length; i++) {
        sum+=this[i];
      return sum;
    };