+ 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

4th Jun 2018, 8:42 PM
BRKOTTA
BRKOTTA - avatar
7 Réponses
+ 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
5th Jun 2018, 2:06 AM
Pao
Pao - avatar
+ 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 👍^^
5th Jun 2018, 4:59 AM
Pao
Pao - avatar
+ 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
4th Jun 2018, 11:31 PM
Pao
Pao - avatar
+ 1
Hi Pao , yes I want it to be includes(). I know about the indexOf, but anyway thank you.
5th Jun 2018, 1:18 AM
BRKOTTA
BRKOTTA - avatar
+ 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.
5th Jun 2018, 4:30 AM
BRKOTTA
BRKOTTA - avatar
+ 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
5th Jun 2018, 4:46 AM
BRKOTTA
BRKOTTA - avatar
+ 1
oh ok, thank you :) Pao
5th Jun 2018, 6:43 AM
BRKOTTA
BRKOTTA - avatar