|
35 | 35 | // compute the most cost effective of the best solutions |
36 | 36 | // |
37 | 37 | mostCostEffective = getMostCostEffectiveSolution(scores, costs, highScore); |
| 38 | + |
| 39 | +// or use the more efficient function: |
| 40 | +//mostCostEffective = getMostCostEffectiveSolution2(bestSolutions, costs, highScore); |
| 41 | + |
| 42 | +// display the results |
38 | 43 | console.log("Bubble Solution #" + mostCostEffective + " is the most cost effective"); |
39 | 44 |
|
40 | 45 | function printAndGetHighScore(scores) { |
|
61 | 66 | } |
62 | 67 |
|
63 | 68 |
|
64 | | -function getMostCostEffectiveSolution(scores, costs, highscore) { |
| 69 | +function getMostCostEffectiveSolution(scores, costs, highScore) { |
65 | 70 | var cost = 100.00; // much higher than any of the costs |
66 | 71 | var index; |
67 | 72 |
|
68 | 73 | for (var i = 0; i < scores.length; i++) { |
69 | | - if (scores[i] == highscore) { |
| 74 | + if (scores[i] == highScore) { |
70 | 75 | if (cost > costs[i]) { |
71 | 76 | index = i; |
72 | 77 | cost = costs[i]; |
|
81 | 86 | // and use the index stored there to find the cost value of that solution. |
82 | 87 | // This is a little more efficient, but not quite as easy to read! |
83 | 88 | // |
84 | | -function getMostCostEffectiveSolution2(bestSolutions, costs, highscore) { |
| 89 | +function getMostCostEffectiveSolution2(bestSolutions, costs, highScore) { |
85 | 90 | var cost = 100.0; |
86 | 91 | var solutionIndex; |
87 | 92 | var lowCostIndex; |
|
90 | 95 | solutionIndex = bestSolutions[i]; |
91 | 96 | if (cost > costs[solutionIndex]) { |
92 | 97 | lowCostIndex = solutionIndex; |
93 | | - cost = costs[i]; |
| 98 | + cost = costs[solutionIndex]; |
94 | 99 | } |
95 | 100 | } |
96 | 101 | return lowCostIndex; |
|
0 commit comments