|
| 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 | + |
0 commit comments