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.includes #15

Open
Sunny-117 opened this issue Nov 3, 2022 · 1 comment
Open

Array.prototype.includes #15

Sunny-117 opened this issue Nov 3, 2022 · 1 comment

Comments

@Sunny-117
Copy link
Owner

Array.prototype.myIncludes = function (value, start = 0) {
  start = start < 0 ? this.length + start : start;
  for (let i = start; i < this.length; i++) {
    if (this[i] == value || (Number.isNaN(value) && Number.isNaN(this[i])))
      return true;
  }
  return false;
};
@kangkang123269
Copy link

Array.prototype.includes = function(searchElement, fromIndex) {
  // 如果数组为空,则直接返回 false
  if (this.length === 0) {
    return false;
  }

  // 如果未传递 fromIndex 参数,则默认为 0
  fromIndex = fromIndex || 0;

  // 如果 fromIndex 是负数,则将其转换为从数组末尾算起的索引
  if (fromIndex < 0) {
    fromIndex = this.length + fromIndex;
  }

  // 遍历数组,查找是否存在指定元素
  for (var i = fromIndex; i < this.length; i++) {
    if (this[i] === searchElement) {
      return true;
    }
  }

  return false;
}

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

2 participants