DEV Community

VueTraining.net
VueTraining.net

Posted on

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

Quiz time! What's wrong with our customMax function? How do we fix it?

Hint: check out the second image, where we show "customMax" in use

Alt Text

Discussion (1)

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])