Nodejs Array Sub allSubArrays(width)

Here you can find the source of allSubArrays(width)

Method Source Code

Array.prototype.allSubArrays = function allSubArrays(width) {
  var w = width;//from w  ww .  ja v a 2 s  . c  o m
  var allArr = [];
  var i;
  var len;

  if (w) {
    w = Math.min(w, this.length);

    for (i = 0, len = this.length - w + 1; i < len; ++i ) {
      allArr.push(this.slice(i, i + w));
    }
  } else {
    for (w = 1; w < this.length; ++w) {
      allArr = allArr.concat(this.allSubArrays(w));
    }
  }

  return allArr;
};

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

console.log([1, 2, 3, 4].allSubArrays(4)); // [ [1, 2, 3, 4] ]
console.log([1, 2, 3, 4].allSubArrays(5)); // [ [1, 2, 3, 4] ]
console.log([1, 2, 3, 4].allSubArrays(6)); // [ [1, 2, 3, 4] ]

console.log([1, 2, 3, 4].allSubArrays()); // [ [1], [2], [3], [4], [1, 2], [2, 3], [3, 4], [1, 2, 3], [2, 3, 4] ]
console.log([1, 2, 3, 4].allSubArrays(0)); // [ [1], [2], [3], [4], [1, 2], [2, 3], [3, 4], [1, 2, 3], [2, 3, 4] ]

console.log([1, 2, 3, 4, 5, 6].allSubArrays(2)); // [ [1, 2], [2, 3], [3, 4], [4, 5], [5, 6] ]


Array.prototype.maxSumSubArray = function maxSumSubArray() {
  var max;

  var subArr = this.allSubArrays(1);

};

Related

  1. listsub(array, sizediff)
    Array.prototype.listsub = function (array, sizediff) {
        if (!array)
            return null;
      var match = [];
        for (var i = 0; i < this.length; i++) {
            if (this[i].contains(array, sizediff)) {
                match.push(this[i]);
        return match;
    
  2. sub(a2)
    Array.prototype.sub = function(a2) {
      var l1 = this.length;
      var l2 = a2.length;
      var len = (l1 < l2 ? l1 : l2);
      var result = new Array(len);
      for (var i = 0; i < len; i++) {
        result[i] = this[i] - a2[i];
      return result;
    ...
    
  3. sub(other)
    Array.prototype.sub = function(other) {
        console.assert(this.length == other.length, 'sub: len(a) != len(b)');
        var ret = new Array(this.length);
        for(var i = 0; i < ret.length; i++)
            ret[i] = this[i] - other[i];
        return ret;
    
  4. subList(start, end)
    Array.prototype.subList = function(start, end){
      return this.slice(start, end);