How to show a value from array that relates to the value of another array, javascript

后端 未结 6 1104
轻奢々
轻奢々 2021-01-28 19:39

I am trying to have the name show up with the score that relates to that name. So if the highest score is 98 I want Joels name to show up in the display where is says name here.

6条回答
  •  隐瞒了意图╮
    2021-01-28 20:07

    As names and scores arrays have same .length, you can use same i variable to select item within names array as is used to select item in scores.

    Declare name variable

    var name = "";
    

    set name variable

    if (scores[i] > highestScore) {
      highestScore = scores[i];
      name = names[i];
    }
    

    display name variable

    document.getElementById("results")
    .innerHTML = "\nAverage score  is " 
              + average 
              + "\nHigh score = " 
              + name 
              + " here with a score of " 
              + highestScore;
    

提交回复
热议问题