Skip to content

Instantly share code, notes, and snippets.

@clarketm
Created May 23, 2016 23:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clarketm/3934318a0d0854d21e6cbb5c8e23aa7b to your computer and use it in GitHub Desktop.
Save clarketm/3934318a0d0854d21e6cbb5c8e23aa7b to your computer and use it in GitHub Desktop.
Find Min Array (JavaScript)
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