It's a cool refactor example, and its an example for a common error ...
The ternary operator nice replacement for the overload, but the correct way would surprise, surprise
x !== undefined ? etc.
'cause this way qs filters out all falshy element from the array and drops there tail too.
Good catch! Without explicitly comparing against undefined, my version would fail for arrays containing zeros. Since this is a sorting algorithm, I'll assume that an array of numbers is being passed in. I'll update to correct it!
Sorry, i just replaced your original bug with a bigger one. And maybe i didn't emphasized enough, that those zeros are not just the zeros. Boolean false, empty string and even the document.all (in browser it's an array or object with type of "undefined" – what?) is falsy...
Yeah, that's true. But I guess what should you expect if you try to put a boolean, empty string, or an object in a number sorting algorithm... There is a reason why Array.sort coerces all items in the array to strings.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
It's a cool refactor example, and its an example for a common error ...
The ternary operator nice replacement for the overload, but the correct way would
surprise, surprise
x !== undefined ? etc.
'cause this way qs filters out all falshy element from the array and drops there tail too.
Good catch! Without explicitly comparing against undefined, my version would fail for arrays containing zeros. Since this is a sorting algorithm, I'll assume that an array of numbers is being passed in. I'll update to correct it!
Sorry, i just replaced your original bug with a bigger one. And maybe i didn't emphasized enough, that those zeros are not just the zeros. Boolean false, empty string and even the document.all (in browser it's an
arrayor object with type of "undefined" – what?) is falsy...Yeah, that's true. But I guess what should you expect if you try to put a boolean, empty string, or an object in a number sorting algorithm... There is a reason why
Array.sort
coerces all items in the array to strings.