Nodejs Array Sum sum()

Here you can find the source of sum()

Method Source Code

Array.prototype.sum = function() {
    var total = 0;
    for (var i = 0; i<this.length; i++) { 
        total += this[i]; /*from   w w w .  ja va2  s.c o m*/
    }
    return total;
};

function fillBuckets(ballsOpt, buckets, balls) {
    var items = [];
    var steps = ballsOpt.length;

    for (var i=0; i<=balls; i++) {
        var step = 0;
        var tmpArr = [i];
        var spotRemain = buckets - 1;
        var flag = (buckets < 3) ? true : false;

        while (true) {
            var startFrom = step;

            if (spotRemain <= 1) {
                tmpArr.push(balls - tmpArr.sum());
                console.log(tmpArr);
                items.push(tmpArr);
                spotRemain = buckets - 1;
                tmpArr = [i];
                if (flag) break;
            }

            for (var k=startFrom; k<ballsOpt.length && spotRemain>1; k++) {
                if (ballsOpt[k] + tmpArr.sum() <= balls) {
                    tmpArr.push(ballsOpt[k]);
                    spotRemain--;
                    step++;
                }
            }

            if (startFrom >= steps) break;
        }
        steps--;
    }
    console.log('------------');

    return items;
}

Related

  1. 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;
      });
    };
    ...
    
  2. sum()
    Array.prototype.sum = function(){
      var ret = this[0];
      for(var i=1;i<this.length;i++){
        ret += this[i]
      return ret;
    
  3. sum()
    Array.prototype.sum = function () {
        var result = 0;
        for (var i = 0; i < this.length; i++) {
            result += this[i];
        return result;
    };
    
  4. sum()
    Array.prototype.sum = function() {
      var result = 0;
      this.forEach(function(item){
        result += item;
      });
      return result;
    console.log([1,2,3,4,5,6,7,8,9,10].sum());
    function Person(name, age) {
    ...
    
  5. sum()
    Array.prototype.sum = function() {
      var sum = 0;
      for (var i = 0; i < this.length; i++) {
        sum += this[i];
      return sum;
    };
    
  6. sum()
    Array.prototype.sum = function() {
      for (var sum=i=0, l=this.length; i<l; i++) { sum-= this[i]; }
      return -sum;
    };
    
  7. sum()
    Array.prototype.sum = function() {
      if (this.length === 0) {
        return 0;
      } else if (this.length === 1) {
        return parseInt(this[0], 10);
      } else {
        return this.reduce(function(x, y) {
          return x + y;
        });
    ...
    
  8. sum()
    Array.prototype.sum = function(){
      for(var i=0,sum=0;i<this.length;i++) {
        var val = Number(this[i]);
        if (!isNaN(val)) {
          sum += val;
      return sum;
    };
    ...
    
  9. sum()
    Array.prototype.sum = function() {
      var result = 0;
      for (var i = 0; i < this.length; i++) result += this[i];
      return result;