Skip to content

Instantly share code, notes, and snippets.

@alex-cory
Created July 29, 2017 18:45
Show Gist options
  • Save alex-cory/63cfb7198b4accdc6a95bd42507e2c8e to your computer and use it in GitHub Desktop.
Save alex-cory/63cfb7198b4accdc6a95bd42507e2c8e to your computer and use it in GitHub Desktop.
Get Closest Number To
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