Created
June 11, 2014 05:45
-
-
Save leohxj/fe2f0c38686d4940c344 to your computer and use it in GitHub Desktop.
给定一个非空的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
// 1 | |
function indexOfSmallest(a) { | |
var lowest = 0; | |
for (var i = 1; i < a.length; i++) { | |
if (a[i] < a[lowest]) lowest = i; | |
} | |
return lowest; | |
} | |
// 2 | |
function indexOfSmallest(a) { | |
return a.reduce(function(lowest, next, index) { | |
return next < a[lowest] : index ? lowest; }, | |
0); | |
} | |
// 3 | |
function indexOfSmallest(a) { | |
return a.indexOf(Math.min.apply(Math, a)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment