Skip to content

Instantly share code, notes, and snippets.

@mytholog
Created March 6, 2017 01:52
Show Gist options
  • Save mytholog/39cb5ff42916103e50a007f5fd0eece7 to your computer and use it in GitHub Desktop.
Save mytholog/39cb5ff42916103e50a007f5fd0eece7 to your computer and use it in GitHub Desktop.
closest number out of array
const counts = [4, 9, 15, 6, 2];
const goal = 5;
counts
.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