Skip to content

Instantly share code, notes, and snippets.

@greggnakamura
Last active August 29, 2015 14:02
Show Gist options
  • Save greggnakamura/99a2b176ca7b188d5ff6 to your computer and use it in GitHub Desktop.
Save greggnakamura/99a2b176ca7b188d5ff6 to your computer and use it in GitHub Desktop.
Javascript: Find the index of the smallest element in an array
function indexOfSmallest(a) {
var lowest = 0;
for (var i = 1; i < a.length; i++) {
if (a[i] < a[lowest]) lowest = i;
}
return lowest;
}
var a = [1, 2, 3, 4, 4, 6, 5, 0, 9];
var result = indexOfSmallest(a);
console.log(result); // 7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment