Skip to content

Commit 49be63d

Browse files
committed
adding chapter 4 code
1 parent 8d3bff5 commit 49be63d

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed

chapter4/bubbles.html

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>Bubble Factory Test Lab</title>
5+
<meta charset="utf-8">
6+
<script>
7+
var scores = [60, 50, 60, 58, 54, 54,
8+
58, 50, 52, 54, 48, 69,
9+
34, 55, 51, 52, 44, 51,
10+
69, 64, 66, 55, 52, 61,
11+
46, 31, 57, 52, 44, 18,
12+
41, 53, 55, 61, 51, 44];
13+
var i = 0;
14+
var highScore = 0;
15+
while (i < scores.length) {
16+
console.log("Bubble solution #" + i + " score: " + scores[i]);
17+
if (scores[i] > highScore) {
18+
highScore = scores[i];
19+
}
20+
i = i + 1;
21+
}
22+
console.log("Bubbles tests: " + scores.length);
23+
console.log("Highest bubble score: " + highScore);
24+
25+
for (var i = 0; i < scores.length; i++) {
26+
console.log("Bubble solution #" + i + " score: " + scores[i]);
27+
if (scores[i] > highScore) {
28+
highScore = scores[i];
29+
}
30+
i = i + 1;
31+
}
32+
console.log("Bubbles tests: " + scores.length);
33+
console.log("Highest bubble score: " + highScore);
34+
35+
var bestSolutions = [];
36+
for (var i = 0; i < scores.length; i++) {
37+
if (scores[i] == highScore) {
38+
bestSolutions.push(i);
39+
}
40+
}
41+
console.log("Solutions with the highest score: " + bestSolutions);
42+
</script>
43+
</head>
44+
<body>
45+
</body>
46+
</html>
47+

chapter4/bubbles2.html

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>Bubble Factory Test Lab</title>
5+
<meta charset="utf-8">
6+
<script>
7+
var scores = [60, 50, 60, 58, 54, 54,
8+
58, 50, 52, 54, 48, 69,
9+
34, 55, 51, 52, 44, 51,
10+
69, 64, 66, 55, 52, 61,
11+
46, 31, 57, 52, 44, 18,
12+
41, 53, 55, 61, 51, 44];
13+
var costs = [.25, .27, .25, .25, .25, .25, .33, .31,
14+
.25, .29, .27, .22, .31, .25, .25, .33,
15+
.21, .25, .25, .25, .28, .25, .24, .22,
16+
.20, .25, .30, .25, .24, .25, .25, .25,
17+
.27, .25, .26, .29];
18+
19+
var highScore, bestSolutions;
20+
21+
// compute the high score and display results
22+
highScore = printAndGetHighScore(scores);
23+
console.log("Bubbles tests: " + scores.length);
24+
console.log("Highest bubble score: " + highScore);
25+
26+
// compute the best solutions and display
27+
bestSolutions = getBestResults(scores, highScore);
28+
console.log("Solutions with the highest score: " + bestSolutions);
29+
30+
// compute the most cost effective of the best solutions
31+
mostCostEffective = getMostCostEffectiveSolution(scores, costs, highScore);
32+
console.log("Bubble Solution #" + mostCostEffective + " is the most cost effective");
33+
34+
function printAndGetHighScore(scores) {
35+
var highScore = 0;
36+
var output;
37+
for (var i = 0; i < scores.length; i++) {
38+
output = "Bubble solution #" + i + " score: " + scores[i];
39+
console.log(output);
40+
if (scores[i] > highScore) {
41+
highScore = scores[i];
42+
}
43+
}
44+
return highScore;
45+
}
46+
47+
function getBestResults(scores, highScore) {
48+
var bestSolutions = [];
49+
for (var i = 0; i < scores.length; i++) {
50+
if (scores[i] == highScore) {
51+
bestSolutions.push(i);
52+
}
53+
}
54+
return bestSolutions;
55+
}
56+
57+
58+
function getMostCostEffectiveSolution(scores, costs, highscore) {
59+
var cost = 100.0;
60+
var index;
61+
62+
for (var i = 0; i < scores.length; i++) {
63+
if (scores[i] == highscore) {
64+
if (cost > costs[i]) {
65+
index = i;
66+
cost = costs[i];
67+
}
68+
}
69+
}
70+
return index;
71+
}
72+
73+
//
74+
// Another way to write this is to use the bestSolutions array,
75+
// and use the index stored there to find the cost value of that solution.
76+
// This is a little more efficient, but not quite as easy to read!
77+
//
78+
function getMostCostEffectiveSolution2(bestSolutions, costs, highscore) {
79+
var cost = 100.0;
80+
var solutionIndex;
81+
var lowCostIndex;
82+
83+
for (var i = 0; i < bestSolutions.length; i++) {
84+
solutionIndex = bestSolutions[i];
85+
if (cost > costs[solutionIndex]) {
86+
lowCostIndex = solutionIndex;
87+
cost = costs[i];
88+
}
89+
}
90+
return lowCostIndex;
91+
}
92+
93+
94+
</script>
95+
</head>
96+
<body> </body>
97+
</html>
98+

chapter4/icecream.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<title>Icecream with bubblegum</title>
5+
<meta charset="utf-8">
6+
<script>
7+
8+
var products = ["Choo Choo Chocolate",
9+
"Icy Mint", "Cake Batter",
10+
"Bubblegum"];
11+
var hasBubbleGum = [false,
12+
false,
13+
false,
14+
true];
15+
for (var i = 0; i < hasBubbleGum.length; i = i + 1) {
16+
if (hasBubbleGum[i] == true) {
17+
console.log(products[i] + " contains bubble gum");
18+
}
19+
}
20+
</script>
21+
</head>
22+
<body></body>
23+
</html>

0 commit comments

Comments
 (0)