Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
Is Array.sort disabled?
Ps. Nah... I had it wrong.
Thanks!
Immutability is hip for a reason: not having side effects prevents bugs.
I'd argue that
Array.prototype.sort
's purpose is not returning a value; its purpose is sorting its argument in place ( probably for performance reasons ), and therefore should not return a value as well - it should have one effect and not two. People tend to usesomething.sort()
directly and forget this also sortssomething
in place, often leading to bugs.MDN "Enumerability and ownership of properties":
Testing sometimes uses
for..in
( or equivalent ) instead offor..of
( or equivalent ), which leads to unexpected results when extendingArray
the wrong way ( it's legal, but for exactly this reason I'm actually calling it wrong ). I have been bitten by this ( because I'm lazy sometimes ).Added.
And no, you're right to not assume it, because the default
.sort
method modify the original array.Could you elaborate that part?
Just wondering, why shouldn't we modify the original array? Sort() method modifies the original array. After all, that's the purpose of this method - to return modified (sorted) array, right? Hence I assumed that our sortReloaded() method should mutate the original array as well.
Still misunderstood what parameter it takes so it returns unmodified array?
I'd add in description that it should not modify the original array. I got to know it from the tests. Maybe I'm stupid that I didn't assume it, I'd add it though :D
It shouldn't sort the elements in-place.
I just got it, it says our method should return a new array
NaN
is not a finite number. Closing.Can someone explain in what condition our method should not modify an array? thanks.
Congratulations, the 1st step to a remarkable future
Loading more items...