Javascript Array range(start, end)

Description

Javascript Array range(start, end)


Array.prototype.range = function (start, end) {
    var total = [];

    if (!end) {//  w ww  .j  a va  2  s.co m
        end = start;
        start = 0;
    }

    for (var i = start; i < end; i++) {
        total.push(i);
    }

    return total;
};

Javascript Array range(start, end)

Array.prototype.range = function(start, end) {
  if (this.length === 0) { return null; }


};

Javascript Array range(start, end)

const arr = [1,2,3,4,5];// w ww  .  ja v  a 2 s .  c  o  m

Array.prototype.range = function (start, end) {
  if (end < start) {
    return [];
  }
  return this.slice(start, end+1);
};

Array.prototype.recur_range = function (start, end) {
  if (end < start) {
    return [];
  }
  if (this.length === 1) {
    return this;
  }

  return this[start].concat(recur_range(start+1, end));
};

// console.log(arr.range(1,3));
// console.log(arr.recur_range(1,3));



PreviousNext

Related