+ 2
Need Explanation
Can someone please explain the provided code below? I tried so hard, and still trying, but, it's just too hard. It was brought from the MDN webpage. It is the polyfill of the Array.prototype.includes() property. What I'm trying to do is, modify the .includes() property so it gives the index of the item instead of just returning true, so, I think understanding the polyfill code is the hardest part of the issue. Thank you in advance. Anyone's help will be appreciated. https://code.sololearn.com/WV2ybY7cHIZ6/#js
7 ответов
+ 9
Oh ok, then just change line 40, instead of 'return true' write 'return k', like this (It's line 42 in this code because of the comment):
https://code.sololearn.com/WmT33zud41ip/?ref=app
In short:
- Variable 'o' holds the array
- Variable 'n' holds the index using the function fromIndex (you should probably search for how this one is built)
- Variable 'k' holds the index passed by variable 'n' if n is greater than or equal to zero, so you just need to print k
+ 6
BRKOTTA oh you're right I was testing it in an old android where includes() is not supported, that's why my modified code's output was correct in my phone😊 but I'm glad you changed the name to make it work 👍^^
+ 5
Hi BRKOTTA 👋 does it have to be includes()? Because if your goal is to get the index of an item in an array, you can use the indexOf() method instead 😊
Reference: https://www.w3schools.com/jsref/jsref_indexof_array.asp
+ 1
Hi Pao , yes I want it to be includes().
I know about the indexOf, but anyway thank you.
+ 1
Thank you very much Pao
The explanation is good but, the code you provided doesn't work as expected, I think because it refers to the actual method Array.prortotype.includes() and not the one that we just modified.
+ 1
UPDATE: never mind Pao I fixed it, the problem was as sated above, so I had to change the name of the new created method, because we can't have too methods of the same name.
Thank you very much for your help.
Edit: here is the code if you wanna see the final result
https://code.sololearn.com/Wc3EG1hsCNFH/?ref=app
+ 1
oh ok, thank you :) Pao