Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array.prototype.fill #14

Open
Sunny-117 opened this issue Nov 3, 2022 · 2 comments
Open

Array.prototype.fill #14

Sunny-117 opened this issue Nov 3, 2022 · 2 comments

Comments

@Sunny-117
Copy link
Owner

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));
@kangkang123269
Copy link

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;
}

@Liboq
Copy link

Liboq commented Sep 15, 2023

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, ));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants