forked from bethrobson/Head-First-JavaScript-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubbles2.html
More file actions
109 lines (93 loc) · 2.67 KB
/
bubbles2.html
File metadata and controls
109 lines (93 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Bubble Factory Test Lab</title>
<script>
var scores = [60, 50, 60, 58, 54, 54,
58, 50, 52, 54, 48, 69,
34, 55, 51, 52, 44, 51,
69, 64, 66, 55, 52, 61,
46, 31, 57, 52, 44, 18,
41, 53, 55, 61, 51, 44];
var costs = [.25, .27, .25, .25, .25, .25, .33, .31,
.25, .29, .27, .22, .31, .25, .25, .33,
.21, .25, .25, .25, .28, .25, .24, .22,
.20, .25, .30, .25, .24, .25, .25, .25,
.27, .25, .26, .29];
var highScore, bestSolutions;
//
// compute the high score and display results
//
highScore = printAndGetHighScore(scores);
console.log("Bubbles tests: " + scores.length);
console.log("Highest bubble score: " + highScore);
//
// compute the best solutions and display
//
bestSolutions = getBestResults(scores, highScore);
console.log("Solutions with the highest score: " + bestSolutions);
//
// compute the most cost effective of the best solutions
//
mostCostEffective = getMostCostEffectiveSolution(scores, costs, highScore);
// or use the more efficient function:
//mostCostEffective = getMostCostEffectiveSolution2(bestSolutions, costs);
// display the results
console.log("Bubble Solution #" + mostCostEffective + " is the most cost effective");
function printAndGetHighScore(scores) {
var highScore = 0;
var output;
for (var i = 0; i < scores.length; i++) {
output = "Bubble solution #" + i + " score: " + scores[i];
console.log(output);
if (scores[i] > highScore) {
highScore = scores[i];
}
}
return highScore;
}
function getBestResults(scores, highScore) {
var bestSolutions = [];
for (var i = 0; i < scores.length; i++) {
if (scores[i] == highScore) {
bestSolutions.push(i);
}
}
return bestSolutions;
}
function getMostCostEffectiveSolution(scores, costs, highScore) {
var cost = 100; // much higher than any of the costs
var index;
for (var i = 0; i < scores.length; i++) {
if (scores[i] == highScore) {
if (cost > costs[i]) {
index = i;
cost = costs[i];
}
}
}
return index;
}
//
// Another way to write this is to use the bestSolutions array,
// and use the index stored there to find the cost value of that solution.
// This is a little more efficient, but not quite as easy to read!
//
function getMostCostEffectiveSolution2(bestSolutions, costs) {
var cost = 100;
var solutionIndex;
var lowCostIndex;
for (var i = 0; i < bestSolutions.length; i++) {
solutionIndex = bestSolutions[i];
if (cost > costs[solutionIndex]) {
lowCostIndex = solutionIndex;
cost = costs[solutionIndex];
}
}
return lowCostIndex;
}
</script>
</head>
<body> </body>
</html>