Created
August 6, 2019 10:02
-
-
Save renatodeleao/df12c8d934dc652a69791bc23877a3a8 to your computer and use it in GitHub Desktop.
closest in range
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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