findClosestNumber.tsx 351 B

12345678
  1. // Finds the closest match to the number from the provided numbers array
  2. export function findClosestNumber(number: number, numbersArray: number[]) {
  3. return numbersArray.reduce((previousBest: number, currentNumber) => {
  4. return Math.abs(currentNumber - number) < Math.abs(previousBest - number)
  5. ? currentNumber
  6. : previousBest;
  7. });
  8. }