forked from bethrobson/Head-First-JavaScript-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubbles.html
More file actions
75 lines (67 loc) · 1.6 KB
/
bubbles.html
File metadata and controls
75 lines (67 loc) · 1.6 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
<!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];
//
// with a while loop
//
var i = 0;
var highScore = 0;
while (i < scores.length) {
console.log("Bubble solution #" + i + " score: " + scores[i]);
if (scores[i] > highScore) {
highScore = scores[i];
}
i = i + 1;
}
console.log("Bubbles tests: " + scores.length);
console.log("Highest bubble score: " + highScore);
//
// with a for loop
//
for (var i = 0; i < scores.length; i++) {
console.log("Bubble solution #" + i + " score: " + scores[i]);
if (scores[i] > highScore) {
highScore = scores[i];
}
}
console.log("Bubbles tests: " + scores.length);
console.log("Highest bubble score: " + highScore);
//
// find the best solution
//
var bestSolutions = [];
for (var i = 0; i < scores.length; i++) {
if (scores[i] == highScore) {
bestSolutions.push(i);
}
}
console.log("Solutions with the highest score: " + bestSolutions);
// Actually Frank was right; you can do it one loop.
// But it's more complex. Here's how:
/*
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];
bestSolutions = [i];
} else if (scores[i] == highScore){
bestSolutions.push(i);
}
}
console.log("Best solutions: " + bestSolutions);
*/
</script>
</head>
<body>
</body>
</html>