Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

find a number that is closest to a specific number in javascript

var counts = [4, 9, 15, 6, 2],
  goal = 5;

var closest = counts.reduce(function(prev, curr) {
  return (Math.abs(curr - goal) < Math.abs(prev - goal) ? curr : prev);
});

console.log(closest);
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #find #number #closest #specific #number #javascript
ADD COMMENT
Topic
Name
2+9 =