開発環境
- OS X Mavericks - Apple(OS)
- Safari, Firefox + Firebug, Google Chrome(Webプラウザ、プラグイン)
- BBEdit - Bare Bones Software, Inc. (GUI) , Emacs (CUI) (Text Editor)
- JavaScript (プログラミング言語)
Head First JavaScript Programming (Eric T. Freeman (著)、 Elisabeth Robson (著)、 O'Reilly Media )のChapter 4(Putting Some Order in Your Data: Arrays)、EXERCISE(p.164)を解いてみる。
EXERCISE(p.164)
コード(BBEdit, Emacs)
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], 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], printAndGetHighScores = function (scores) { var highScore = - Infinity, output, i, max, score; for (i = 0, max = scores.length; i < max; i += 1) { score = scores[i]; output = 'Bubble solution #' + i + ' score: ' + score; print(output); if (score > highScore) { highScore = score; } } return highScore; }, getMostCostEffectiveSolution = function (scores, costs, highScore) { var cost = 100, index, i, max; for (i = 0, max = scores.length; i < max; i += 1) { if (scores[i] === highScore) { if (cost > costs[i]) { index = i; cost = costs[i]; } } } return index }, highScore, mostCostEffective; highScore = printAndGetHighScores(scores); mostCostEffective = getMostCostEffectiveSolution(scores, costs, highScore); print('Bubble Solution #' + mostCostEffective + ' is the most cost effective');
0 コメント:
コメントを投稿