DEV Community

loading...

Discussion on: Quiz Time! What's Wrong With This "customMax" function?

Collapse
dploeger profile image
Dennis Ploeger

All non-primitives are passed by reference in es, so we're effectively passing the same array to the sort function. That's why the array is sorted afterwards. The solution would be to clone the array to a new variable before sorting it (i.e. by using the spread operator const larr = [...arr])