Created
January 4, 2023 22:43
-
-
Save codebrainz/a5c3938934e9f0f00a6f33797d4ce08c to your computer and use it in GitHub Desktop.
Find the smallest number in an array in JS
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
function min(arr) { | |
if (!arr) return NaN; | |
let min = Number.MAX_VALUE; | |
for (const element of arr) { | |
if (element < min) { | |
min = element; | |
} | |
} | |
return min; | |
} | |
console.log(min([2, 0, 42, 222, -4096])); | |
console.log(min([])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment