The goal of this problem is to iterate over the list, find the highest value in the list, and then report the index values ββfor the highest values. I was able to solve this problem using two for loops:
var scores = [60, 50, 58, 54, 54, 58, 50, 52, 54, 48, 69, 34, 55, 51, 52, 44, 51, 69, 64, 66, 55, 52, 44, 18, 41, 53, 55, 61, 51, 44]; var highscore = 0; var highscoreSolutions = []; for (var i = 0; i < scores.length; i++){ if (scores[i] > highscore){ highscore = scores[i]; } } for (var i = 0; i < scores.length; i++){ if (scores[i] == highscore){ highscoreSolutions.push(i); } } console.log(highscore); console.log(highscoreSolutions);
At first I tried to solve this problem using only one cycle, but I ran into the problem of initializing sortings, that is, regardless of the fact that the first index value will be included in the list of the highest scores:
var scores = [60, 50, 58, 54, 54, 58, 50, 52, 54, 48, 69, 34, 55, 51, 52, 44, 51, 69, 64, 66, 55, 52, 44, 18, 41, 53, 55, 61, 51, 44]; var highscore = 0; var highscoreSolutions = []; for (var i = 0; i < scores.length; i++){ if (scores[i] >= highscore){ highscore = scores[i]; highscoreSolutions.push(i); } } console.log(highscore); console.log(highscoreSolutions);
I'm not sure how to get around the problem of adding an index value of 0 (without resorting to using two separate loops). Can someone help me? Thanks!!:)