Created
July 29, 2017 18:45
-
-
Save alex-cory/63cfb7198b4accdc6a95bd42507e2c8e to your computer and use it in GitHub Desktop.
Get Closest Number To
This file contains hidden or 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
var closest = (nums, goal) => nums.reduce((prv, cur) => ( | |
Math.abs(prv - goal) > Math.abs(cur - goal) ? cur : prv | |
)) | |
closest([-1, 2, 1, -4], 3) // should return 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment