1

Ok so i need to recreate the find method but i cant manage to suceed. I am trying to do this but everytime i run this on a array it simple returns me an error

  array.prototype.findDaShopee = function ( ok ) {
    for (let i = 0; i < this.length; i++) {
        if (this[i] == ok[i]) {
            return this[i]
        })
    }

  }

but it's not working

I tried removing the 'ok' function, i tried everything, i dont want the answer just the explanation

i'm really tring to understand

  • 2
    The argument to the `find()` method is a function that should be called, not an array. – Barmar Dec 08 '22 at 01:31
  • Please provide a [mcve] containing the code that produces the error, and what the error in question actually is. Based solely on what you've provided here, there could be multiple different problems you're encountering. – Patrick Roberts Dec 08 '22 at 01:33
  • you are using loose equality here, if you really want to do that read this comparison between `==` and `===` [read this](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness) – Rajesh Kanna Dec 08 '22 at 01:33

1 Answers1

3

The name of the class is Array, not array.

ok is not an array, it's a function that should be called on each element of the array. The method returns the first element where the function returns a truthy value.

You had an extra ) in the code.

Array.prototype.findDaShopee = function(ok) {
  for (let i = 0; i < this.length; i++) {
    if (ok(this[i])) {
      return this[i]
    }
  }
}

const testArray = [1, 2, 10, 12];
console.log(testArray.findDaShopee(el => el % 5 == 0));
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    Maybe it's worth mentioning (or linking to something else that explains) that `Array.prototype.anythingHere = ...` is bad practice, because it makes the method enumerable in `for...in` loops (which are also bad practice) – Patrick Roberts Dec 08 '22 at 01:37
  • This is obviously just a toy exercise, I don't think it's worth it. – Barmar Dec 08 '22 at 01:38
  • @PatrickRoberts Can you point me to a resource explaining why `for...in`-loops are considered bad practice? I haven't really used it yet (`Object.keys()` works fine _mostly_), but I don't understand why it may be bad practice. – Oskar Grosser Dec 08 '22 at 03:44
  • @OskarGrosser See https://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-such-a-bad-idea?lq=1 – Barmar Dec 08 '22 at 04:33
  • [This answer](https://stackoverflow.com/a/4261096/13561410) explains that `for...in`-loops may be misused as `for...of`-loops, where _the misuse_ is bad practice. But it doesn't consider `for...in`-loops themselves as bad practice (and neither do I). Thank you for the link! – Oskar Grosser Dec 08 '22 at 19:11
  • 1
    There are dozens of other answers giving other reasons why it's not a good idea. – Barmar Dec 08 '22 at 20:23
  • @OskarGrosser within the context of my comment, I was specifically referring to the use of `for...in` on arrays, but I also feel the semantics of `for...in` are confusing especially when considering poorly written libraries that improperly modify builtin prototypes like this question does. In contrast `Object.keys` is restricted to _own_ enumerable properties, so modifications to the prototype chain like this example won't modify what it returns. – Patrick Roberts Dec 08 '22 at 20:24