Skip to content

Instantly share code, notes, and snippets.

@renatodeleao
Created August 6, 2019 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renatodeleao/df12c8d934dc652a69791bc23877a3a8 to your computer and use it in GitHub Desktop.
Save renatodeleao/df12c8d934dc652a69791bc23877a3a8 to your computer and use it in GitHub Desktop.
closest in range
/**
* @desc gets the closes number in a range of numbers
* @param {Array} list
* @param {Number} goal
* @author a mix of stackoverflowers
*/
const closestInRange = (list, goal) => (
list.reduce((prev, curr) => (
Math.abs(curr - goal) < Math.abs(prev - goal) ? curr : prev
))
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment