Hello guys, from the time i started Javascript this question has been bothering me a lot and i just cant a good answer anywhere.
So how do we know How many and Which arguments a callback function takes? for example -
while doing the array.map tutorial of FCC it showed that it takes a callback function with currentValue as argument.
But now when i was doing a challenge on CodeWars i ran into a problem that needed me to use array.map but i didnt know its callback function could take index as an argument too.
In addition to what @joesmith100 said, many code editors and IDEs provide calltips that inform you what parameters a function takes and what it returns. Something like this: getElementById(id: str).
Yeah, in the case of map this looks pretty intimidating. But I think it can still be quite helpful. I’m pretty sure you’ll learn how to read those hints.
@janschreiber how can i learn to read those hints , if there are no documentation.
I’m really very sorry for bothering you again and again but im sure other campers run into this problem a lot as well.
I’d suggest that whenever you encounter something that doesn’t make sense to you, look up the official documentation and try to match it with what you see in the calltip.
In our example, w3schools says the syntax is array.map(function(currentValue, index, arr), thisValue)
So, the first argument fn is apparently a function that takes elt, i and array as arguments, context is optional (?) and can be of any type (?), while i must be a number and array is, well, an array. .map() is probably one of the worst examples for explaining this. For a calltip like getElementById(id: str), you can conclude that there is one required parameter, id, which has to be a string. Those are basically just abbreviations.
highly grateful for all you help , your .map comparison with w3School does make a lot of sense, i’ll keep doing this from now on and one last problem
when i do document.getElementById() or document.querySelector and a lot more , i dont get the Autocomplete/ternJS guide, i tried reinstalling Atom + every Plugin , and even tried on different PC but no luck am i missing something or maybe i messed something ?
@janschreiber nope i’m not talking about jQuery but thanks a lot for all your help. @joesmith100 too Thank you both, I hope we can someday collaborate and acheive awesomeness.