Skip to content

Instantly share code, notes, and snippets.

@jafar-jabr
Created January 26, 2022 15:37
Show Gist options
  • Save jafar-jabr/c5cb324751f9b2fc46be30c6c6408731 to your computer and use it in GitHub Desktop.
Save jafar-jabr/c5cb324751f9b2fc46be30c6c6408731 to your computer and use it in GitHub Desktop.
closest to zero
const closestToZero = (_arr) => {
let closest = _arr[0] ?? 0;
_arr.forEach((item) => {
if(item <= Math.abs(closest) && item > 0){
closest = item;
}else if(Math.abs(item) < Math.abs(closest)){
closest = item;
}
});
return closest;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment