In this case, I use two parallel arrays ( cost[] and scores[] ), both of which have data on them that are parallel to each other.
This code is correct as I copy it from the book I use. What I am not getting is how it can work for a loop for an array of costs. I get that we pass both arrays as parameters in the function, but in the for loop there is only scores.length , so there shouldn't be another loop for cost.lenght ?
function getMostCostEffectiveSolution(scores, costs, highScore) var cost = 100; var index; for (var i = 0; i < scores.length; i++) { if (scores[i] == highScore) { if(cost > cost[i]) { index = i; cost = cost[i]; } } } return index; }
source share