Method to compare which one is the more close We find the closest by taking the difference between the target and both values . It assumes that val2 is greater than val1 and target lies between these two .
function getClosest ( val1 , val2 , target ) { if ( target - val1 >= val2 - target ) return val2 ; else return val1 ; }