Nodejs Array Sum sum()

Here you can find the source of sum()

Method Source Code

/*/*  ww  w  .j  a va  2 s .  c  om*/
The JavaScript standard now includes functional additions to array like map, 
filter and reduce, but sadly is missing the convenience functions range and sum . 
Implement a version of range and sum (which you can then copy and use in your future Kata to make them smaller).

Array.range(start, count) should return an array containing 'count' numbers from
 'start' to 'start + count' Example: Array.range(0, 3) returns [0, 1, 2]

Array.sum() return the sum of all numbers in the array 
Example: [0, 1, 2].sum() returns 3 Example: Array.range(-1,4).sum() should return 2

While not forbidden try to write both function without using a for loop
*/

Array.range = function(start, count) {
  var newArray = [];
  
  for( var i = 0, j = start; i< count; i++, j++)
    newArray.push(j);
  
  return newArray;
}

Array.prototype.sum = function () {
    var sum = 0;
    this.forEach(function (value) {
        sum += value;
    });
    return sum;
}

Related

  1. sum()
    Math.TAU = Math.PI * 2;
    Array.prototype.sum = function()
      for (var i = 0, L = this.length, sum = 0; i < L; sum += this[i++]);
      return sum;
    Array.prototype.product = function()
      for (var i = 0, L = this.length, product = 1; i < L; product =  product * this[i++]);
    ...
    
  2. sum()
    Array.prototype.sum = function(){
      return this.reduce(function(sum, item){
        return sum + item;
      }, 0)
    function isInt(n){
        return Number(n) === n && n % 1 === 0;
    function isFloat(n){
    ...
    
  3. sum()
    Array.prototype.sum = function() {
      var sum = 0;
      this.forEach(function(k) {
        sum += k;
      });
      return sum;
    };
    
  4. sum()
    Array.prototype.sum = function()
      if (this.length < 1) {
        return Number.NaN;
      var sum = 0;
      this.forEach(function(a) {
        sum += a;
      });
    ...
    
  5. sum()
    Array.prototype.sum = function () {
        return this.reduce(function(previousValue, currentValue) {
            return previousValue + currentValue;
        });
    };
    
  6. sum()
    Array.prototype.sum = function () {
      var t = 0;
      for (var i = 0; i < this.length; i++ ) {
        t += this[i];
      return t;
    
  7. sum()
    var val = [1, 2, 3];
    function array(val){
      this.val = val;
    Array.prototype.sum = function(){
      return this.reduce(function(a, b){
        return a + b;
      });
    };
    ...
    
  8. sum()
    Array.prototype.sum = function(){
      var ret = this[0];
      for(var i=1;i<this.length;i++){
        ret += this[i]
      return ret;
    
  9. sum()
    Array.prototype.sum = function () {
        var result = 0;
        for (var i = 0; i < this.length; i++) {
            result += this[i];
        return result;
    };