Created
January 26, 2022 15:37
-
-
Save jafar-jabr/c5cb324751f9b2fc46be30c6c6408731 to your computer and use it in GitHub Desktop.
closest to zero
This file contains 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
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