Created
December 19, 2013 16:25
-
-
Save demux/8042026 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function nearestInList(number, list, returnDistance) { | |
var distance = Infinity; | |
var n = null; | |
for(var i in list) { | |
var _distance = Math.abs(list[i] - number); | |
if(_distance < distance) { | |
distance = _distance; | |
n = list[i]; | |
} | |
} | |
if(returnDistance === true) { | |
return distance; | |
} | |
return n; | |
} | |
function nearSort(n, list) { | |
return lst.sort(function(a, b) { | |
return Math.abs(a - n) - Math.abs(b - n); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment