Nodejs Array Sum sum()

Here you can find the source of sum()

Method Source Code

Array.prototype.sum = function () {
  return this.reduce(function(sum, val){ 
    return sum + val
  })/*from   w  w w  . j  a v a  2 s .co  m*/
}

console.log([1, 2, 3, 4].sum());

Related

  1. 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;
    };
    ...
    
  2. 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());
    
  3. 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());
    
  4. 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;
    };
    ...
    
  5. 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];
    ...
    
  6. sum()
    Array.prototype.sum = function() {
      return this.reduce((result, element) => result + element, 0);
    };
    
  7. 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);
    ...
    
  8. 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);
    ...
    
  9. sum()
    Array.prototype.sum = function(){
      var val = 0;
      this.forEach(function(v){
        val +=v;
      });
      return val;
    };