Sunny-117 / js-challenges

✨✨✨ Challenge your JavaScript programming limits step by step

Home Page:https://juejin.cn/column/7244788137410560055

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Array.prototype.fill

Sunny-117 opened this issue · comments

Array.prototype.myFill = function (initValue, start = 0, end) {
  end = end < 0 ? this.length + end : end;
  for (let i = start; i < end; i++) {
    this[i] = initValue;
  }
  return this;
};
let arr = [3, 3, 21, 3, 14, 12, 4, 1, 2];
console.log(arr.fill(1, 3, 5));
Array.prototype.fill = function(value, start, end) {
  var len = this.length;
  start = start || 0;
  end = end || len;
  start = (start >= 0) ? start : Math.max(len + start, 0);
  end = (end >= 0) ? Math.min(end, len) : len + end;
  for (var i = start; i < end; i++) {
    this[i] = value;
  }
  return this;
}
Array.prototype._fill = function (value, start = 0, end = this.length) {
  end = end < 0 ? this.length + end : end
  if (typeof start !== "number" || typeof end !== "number") {
    return this
  }
  for (let i = start; i < end; i++) {
    this[i] = value
  }
  return this
}
const arr = [3, 2, 1, 4]
console.log(arr._fill(1, 1, ));