re: Challenge: Get Closest Number in an Array VIEW POST

VIEW PARENT COMMENT VIEW FULL DISCUSSION
 

I dont think use sort is good idea:
Let see:

[2,3,4,11].sort()
 

By including a function to sort by I'm no longer doing an alphabetical sort, which means this problem no longer exists.

  console.log([2,3,4,11].sort((a,b)=>a-b));
Output:
  (4) [2, 3, 4, 11]

The sort() method sorts the elements of an array in place and returns the array. The default sort order is built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.

developer.mozilla.org/en-US/docs/W...

yes, by including compareFunction callback, we can solve this problem.

code of conduct - report abuse