Created
May 7, 2022 10:10
-
-
Save agnel/c3b47e161ade1defb248f088809327e0 to your computer and use it in GitHub Desktop.
Find min and max values without Math functions 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
/** | |
* These methods only work for an array of numbers | |
*/ | |
function findMinimum(array) { | |
let length = array.length; | |
let min = Infinity; | |
while(length--) { | |
if(array[length] < min) { | |
min = array[length]; | |
} | |
} | |
return min; | |
} | |
function findMaximum(array) { | |
let length = array.length; | |
let max = -Infinity; | |
while(length--) { | |
if(array[length] > max) { | |
max = array[length]; | |
} | |
} | |
return max; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment