可汗学院:在子阵列中找到最小值

时间:2017-01-24 16:27:53

标签: javascript khan-academy

我被困在子阵列中查找最小值的第2步,我找不到另外2个Assert.equal s的值来发现程序是准确的。

我所拥有的内容如下:

var indexOfMinimum = function(array, startIndex) {
// Set initial values for minValue and minIndex,
// based on the leftmost entry in the subarray:  
var minValue = array[startIndex];
var minIndex = startIndex;


//Loop over items starting with startIndex, 
//updating minValue and minIndex as needed:
for(var nextIndex = minIndex + 1; nextIndex < array.length  ; nextIndex ++) {
if(array[nextIndex]<minValue) {
    minIndex = nextIndex;
    minValue =array[nextIndex];
}

}

return minIndex;
}; 

var array = [18, 6, 66, 44, 9, 22, 14];  
//  For the test array [18, 6, 66, 44, 9, 22, 14], 
//  the value 9 is the smallest of [..66, 44, 9, 22, 14]
//  Since 9 is at index 4 in the original array, 
var index = indexOfMinimum(array, 2);
//  "index" has value 4
println("The index of the minimum value of the subarray starting at index 2 is " + index + "."  );
Program.assertEqual(index, 4);
Program.assertEqual(indexOfMinimum(array,?) , ?);
Program.assertEqual(indexOfMinimum(array,?) , ?);

任何帮助?

1 个答案:

答案 0 :(得分:0)

很容易。方法assertEqual断言两个参数是相等的。

Program.assertEqual(indexOfMinimum(array,1) , 1);
Program.assertEqual(indexOfMinimum(array,5) , 6);