Javascript比较奇怪

我遇到了比较的问题。结果假设为 11,因为索引 11 的产品成本低于索引 18 的产品成本。但不知何故结果是 18


    var scores = [60, 50, 60, 58, 54, 54, 58, 50, 52, 54, 48, 69, 34, 55, 51, 52, 44, 51, 69, 64, 66, 55, 52, 61, 46, 31, 57, 52, 44, 18,

    41, 53, 55, 61, 51, 44];

    var costs = [.25, .27, .25, .25, .25, .25, .33, .31, .25, .29, .27, .22, .31, .25, .25, .33, .21, .25, .25, .25, .28, .25, .24, .22, .20, .25, .30, .25, .24, .25,

        .25, .25, .27, .25, .26, .29];

//TOTAL, HIGHEST, INDEX OF THE HIGHEST

function index(array,arrayCost){

    var maximum=Math.max(...array);

    var arrayIndex=[];

    for(var i=0;i<array.length;i++){

        if(array[i]==maximum){

            arrayIndex.push(i)

        }

    }


   var minimum=arrayCost[arrayIndex[0]];//0.22

   for(var i=0;i<arrayIndex.length;i++){

       if(arrayCost[arrayIndex[i]]<=minimum){//0.25>=0.22

           minimum=arrayIndex[i];

       }

   }

    return minimum;

}


猛跑小猪
浏览 99回答 2
2回答

慕尼黑8549860

我遇到了比较的问题。结果假设为 11,因为索引 11 的产品成本低于索引 18 的产品成本。但不知何故结果是 18&nbsp; &nbsp; var scores = [60, 50, 60, 58, 54, 54, 58, 50, 52, 54, 48, 69, 34, 55, 51, 52, 44, 51, 69, 64, 66, 55, 52, 61, 46, 31, 57, 52, 44, 18,&nbsp; &nbsp; 41, 53, 55, 61, 51, 44];&nbsp; &nbsp; var costs = [.25, .27, .25, .25, .25, .25, .33, .31, .25, .29, .27, .22, .31, .25, .25, .33, .21, .25, .25, .25, .28, .25, .24, .22, .20, .25, .30, .25, .24, .25,&nbsp; &nbsp; &nbsp; &nbsp; .25, .25, .27, .25, .26, .29];//TOTAL, HIGHEST, INDEX OF THE HIGHESTfunction index(array,arrayCost){&nbsp; &nbsp; var maximum=Math.max(...array);&nbsp; &nbsp; var arrayIndex=[];&nbsp; &nbsp; for(var i=0;i<array.length;i++){&nbsp; &nbsp; &nbsp; &nbsp; if(array[i]==maximum){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arrayIndex.push(i)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;var minimum=arrayCost[arrayIndex[0]];//0.22&nbsp; &nbsp;for(var i=0;i<arrayIndex.length;i++){&nbsp; &nbsp; &nbsp; &nbsp;if(arrayCost[arrayIndex[i]]<=minimum){//0.25>=0.22&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;minimum=arrayIndex[i];&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;}&nbsp; &nbsp; return minimum;}

慕虎7371278

您的错误显示在评论中,但我会对其进行修改以使用单个循环:var scores = [60, 50, 60, 58, 54, 54, 58, 50, 52, 54, 48, 69, 34, 55, 51, 52, 44, 51, 69, 64, 66, 55, 52, 61, 46, 31, 57, 52, 44, 18,&nbsp; &nbsp; 41, 53, 55, 61, 51, 44];&nbsp; &nbsp; var costs = [.25, .27, .25, .25, .25, .25, .33, .31, .25, .29, .27, .22, .31, .25, .25, .33, .21, .25, .25, .25, .28, .25, .24, .22, .20, .25, .30, .25, .24, .25,&nbsp; &nbsp; &nbsp; &nbsp; .25, .25, .27, .25, .26, .29];function index(array, arrayCost) {&nbsp; var maxScore = Math.max(...array);&nbsp; var minCostIndex = null;&nbsp; for (var i = 0; i < arrayCost.length; i++) {&nbsp; &nbsp; if (array[i] != maxScore) {&nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; }&nbsp; &nbsp; if (minCostIndex == null || arrayCost[i] < arrayCost[minCostIndex]) {&nbsp; &nbsp; &nbsp; minCostIndex = i;&nbsp; &nbsp; }&nbsp; }&nbsp; return minCostIndex;}console.log("res", index(scores, costs));如果你愿意,你甚至可以消除Math.max:var scores = [60, 50, 60, 58, 54, 54, 58, 50, 52, 54, 48, 69, 34, 55, 51, 52, 44, 51, 69, 64, 66, 55, 52, 61, 46, 31, 57, 52, 44, 18,&nbsp; &nbsp; 41, 53, 55, 61, 51, 44];&nbsp; &nbsp; var costs = [.25, .27, .25, .25, .25, .25, .33, .31, .25, .29, .27, .22, .31, .25, .25, .33, .21, .25, .25, .25, .28, .25, .24, .22, .20, .25, .30, .25, .24, .25,&nbsp; &nbsp; &nbsp; &nbsp; .25, .25, .27, .25, .26, .29];function index(array, arrayCost) {&nbsp; var maxScore = array[0];&nbsp; var minCostIndex = 0;&nbsp;&nbsp;&nbsp; // start iteration at 1, since 0 is accounted for in the vars&nbsp; for (var i = 1; i < arrayCost.length; i++) {&nbsp; &nbsp; if (array[i] > maxScore) {&nbsp; &nbsp; &nbsp; maxScore = array[i];&nbsp; &nbsp; &nbsp; minCostIndex = i;&nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; }&nbsp; &nbsp; if (array[i] == maxScore && arrayCost[i] < arrayCost[minCostIndex]) {&nbsp; &nbsp; &nbsp; minCostIndex = i;&nbsp; &nbsp; }&nbsp; }&nbsp; return minCostIndex;}console.log("res", index(scores, costs));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript