Last active
August 29, 2015 14:02
-
-
Save greggnakamura/99a2b176ca7b188d5ff6 to your computer and use it in GitHub Desktop.
Javascript: Find the index of the smallest element in an array
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 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