loading...

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

vuetraining profile image VueTraining.net ・1 min read

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

pic
Editor guide
 

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