JavaScript to set iteration of a Loop Array - using one or two loops

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!!:)

+6
source share
3 answers

You need to clear the list when you find the new highest value:

 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 = []; var score; for (var i = 0; i < scores.length; i++) { score = scores[i]; if (score == highscore) { highscore = score; highscoreSolutions.push(i); } else if (score > highscore) { highscore = score; // We have a new highest score, so all the values currently in the array // need to be removed highscoreSolutions = [i]; } } snippet.log(highscore); snippet.log(highscoreSolutions.join(", ")); 
 <!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.imtqy.com/simple-snippets-console/snippet.js"></script> 
+3
source

This may sound overly designed for you, but as you seem to be learning JavaScript, I suggest you skip the for iterations (well, you should know that they exist, and you can use them for a loop) and learn using JavaScript using the functional programming a paradigm (a great interactive tutorial for this ) as it leads to more readable and less error prone code.

A solution to your problem might use [].reduce() :

 function highest(numbers) { return numbers.reduce(function(winner, current, index) { if (current > winner.value) { return {value: current, indices: [index]}; } else if (current === winner.value) { winner.indices.push(index); return winner; } else { return winner; } }, {value: Number.NEGATIVE_INFINITY, indices: []}); } 

 function highest(numbers) { return numbers.reduce(function(winner, current, index) { if (current > winner.value) { return {value: current, indices: [index]}; } else if (current === winner.value) { winner.indices.push(index); return winner; } else { return winner; } }, {value: Number.NEGATIVE_INFINITY, indices: []}); } document.querySelector('button').addEventListener('click', function() { var randoms = new Array(100).join(' ').split('').map(function() {return Math.ceil(Math.random() * 100)}); document.querySelector('#array').innerHTML = JSON.stringify(randoms); document.querySelector('#result').innerHTML = JSON.stringify(highest(randoms)); }); 
 <button>run</button> <h3>the array</h3> <pre id="array"></pre> <h3>the result</h3> <pre id="result"></pre> 
+1
source
 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]; //clone array keeping original as scores, sort the new cloned array, grab the max value var highscore = scores.slice(0).sort()[scores.length - 1]; var highscoreSolutions = []; //find occurances of highscore and push to array for (var i = 0; i < scores.length; i++){ if (scores[i] == highscore){ highscoreSolutions.push(i); } } alert('highscore: ' + highscore + '\nindexes: ' + highscoreSolutions) 

You can clone an array using Array.slice (0)

Then run sort () and take the last value of the new sorted array, checking the length -1

Then repeat the cycle to find the indices in which the records are.

0
source

All Articles