Created
May 23, 2016 23:40
-
-
Save clarketm/3934318a0d0854d21e6cbb5c8e23aa7b to your computer and use it in GitHub Desktop.
Find Min Array (JavaScript)
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 findMin(array) { | |
var min = array[0], | |
count = 0; | |
for (var i = 1; i < array.length; i++, count++) { | |
if (array[i] < min) { | |
min = array[i]; | |
} | |
} | |
console.log(count + " iterations"); | |
return min; | |
} | |
// Examples | |
console.log(findMin([1,76,3,5,0,7,-2])); // min = -2 //=> 6 iterations | |
console.log(findMin([13,6,-9,5])); // min = -9 //=> 3 iterations | |
console.log(findMin([3,6])); // min = 3 //=> 1 iterations | |
console.log(findMin([13,6])); // min = 6 //=> 1 iterations |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment