Comments on: Get the closest next or previous integer in a sorted array of integers http://www.antimath.info/js/get-the-closest-next-or-previous-integer-in-a-sorted-array-of-integers/ thoughts: simple; Fri, 10 Jun 2016 09:05:55 +0000 hourly 1 https://wordpress.org/?v=4.5.3 By: Mihai http://www.antimath.info/js/get-the-closest-next-or-previous-integer-in-a-sorted-array-of-integers/#comment-311882 Sun, 30 Nov 2014 17:24:50 +0000 http://www.antimath.info/?p=477#comment-311882 Thank you for your thoughts and for the fiddle. It’s a very nice method.

]]>
By: Calin http://www.antimath.info/js/get-the-closest-next-or-previous-integer-in-a-sorted-array-of-integers/#comment-311878 Fri, 28 Nov 2014 19:09:53 +0000 http://www.antimath.info/?p=477#comment-311878 You can also use array.reduce to find the closest number. The only problem is that array.reduce doesn’t pass extra parameters to the callback, and you need to pass the “pivot”.

To go around this problem, you could create a class, and store the “pivot” in the instance as a property.

Here is a quick fiddle.

Also, the array doesn’t have to be sorted for this implementation.

]]>