Answers for "find a number that is closest to a specific number in javascript"

0

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);
Posted by: Guest on October-11-2021

Code answers related to "find a number that is closest to a specific number in javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language