Skip to content

Instantly share code, notes, and snippets.

@andreasvirkus
Last active December 5, 2016 14:12
Show Gist options
  • Save andreasvirkus/445feab4dbdadd4d2f632a3d1ef72756 to your computer and use it in GitHub Desktop.
Save andreasvirkus/445feab4dbdadd4d2f632a3d1ef72756 to your computer and use it in GitHub Desktop.
var counts = [4, 9, 15, 6, 2],
goal = 5;
var closest = counts.reduce(function (prev, curr) {
return (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