Can you guys explain this code and how its run ??

Can you guys  explain this code and how its run ??
weirdReverse=a=>a.sort(_=>1)
You already invited:

Denis

Upvotes from:

every array has a sort method, sort expects a compare function, on each iteration, the compare function will receive two arguments (elements of the array) to be compared, we will call them a and b, if the compare function returns a number that is less than 0, it will sort a before b, if it returns more than 0, b will be sorted before a.
If the compare function always returns more than 0 (your case), b will always be sorted before a leading to an interesting reverse behavior.
If you are confused about the underscore, it is just the first argument your compare function will receive, it is what I was referring to a, you could have written it like: weirdReverse= a => a.sort((a, b) => 1), in some languages it's a convention to name the argument as an underscore if we're ignoring it.
MDN Docs on sort: https://developer.mozilla.org/ ... /sort

If you wanna answer this question please Login or Register