第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Javascript比較奇怪

Javascript比較奇怪

猛跑小豬 2023-02-17 16:41:15
我遇到了比較的問題。結果假設為 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 HIGHESTfunction 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;}
查看完整描述

2 回答

?
慕尼黑8549860

TA貢獻1818條經驗 獲得超11個贊

我遇到了比較的問題。結果假設為 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;

}


查看完整回答
反對 回復 2023-02-17
?
慕虎7371278

TA貢獻1802條經驗 獲得超4個贊

您的錯誤顯示在評論中,但我會對其進行修改以使用單個循環(huán):


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];



function index(array, arrayCost) {

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

  var minCostIndex = null;

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

    if (array[i] != maxScore) {

      continue;

    }

    if (minCostIndex == null || arrayCost[i] < arrayCost[minCostIndex]) {

      minCostIndex = i;

    }

  }


  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,

    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];



function index(array, arrayCost) {

  var maxScore = array[0];

  var minCostIndex = 0;

  

  // start iteration at 1, since 0 is accounted for in the vars

  for (var i = 1; i < arrayCost.length; i++) {

    if (array[i] > maxScore) {

      maxScore = array[i];

      minCostIndex = i;

      continue

    }


    if (array[i] == maxScore && arrayCost[i] < arrayCost[minCostIndex]) {

      minCostIndex = i;

    }

  }


  return minCostIndex;

}


console.log("res", index(scores, costs));


查看完整回答
反對 回復 2023-02-17
  • 2 回答
  • 0 關注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號