Skip to content

Commit adebbe5

Browse files
committed
fix to error in getMostCostEffectiveSolution2 function
1 parent 5f75bd7 commit adebbe5

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

chapter4/bubbles2.html

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
// compute the most cost effective of the best solutions
3636
//
3737
mostCostEffective = getMostCostEffectiveSolution(scores, costs, highScore);
38+
39+
// or use the more efficient function:
40+
//mostCostEffective = getMostCostEffectiveSolution2(bestSolutions, costs, highScore);
41+
42+
// display the results
3843
console.log("Bubble Solution #" + mostCostEffective + " is the most cost effective");
3944

4045
function printAndGetHighScore(scores) {
@@ -61,12 +66,12 @@
6166
}
6267

6368

64-
function getMostCostEffectiveSolution(scores, costs, highscore) {
69+
function getMostCostEffectiveSolution(scores, costs, highScore) {
6570
var cost = 100.00; // much higher than any of the costs
6671
var index;
6772

6873
for (var i = 0; i < scores.length; i++) {
69-
if (scores[i] == highscore) {
74+
if (scores[i] == highScore) {
7075
if (cost > costs[i]) {
7176
index = i;
7277
cost = costs[i];
@@ -81,7 +86,7 @@
8186
// and use the index stored there to find the cost value of that solution.
8287
// This is a little more efficient, but not quite as easy to read!
8388
//
84-
function getMostCostEffectiveSolution2(bestSolutions, costs, highscore) {
89+
function getMostCostEffectiveSolution2(bestSolutions, costs, highScore) {
8590
var cost = 100.0;
8691
var solutionIndex;
8792
var lowCostIndex;
@@ -90,7 +95,7 @@
9095
solutionIndex = bestSolutions[i];
9196
if (cost > costs[solutionIndex]) {
9297
lowCostIndex = solutionIndex;
93-
cost = costs[i];
98+
cost = costs[solutionIndex];
9499
}
95100
}
96101
return lowCostIndex;

0 commit comments

Comments
 (0)