2014年8月9日土曜日

開発環境

Head First JavaScript Programming (Eric T. Freeman (著)、 Elisabeth Robson (著)、 O'Reilly Media )のChapter 4(Putting Some Order in Your Data: Arrays)、SHARPEN YOUR PENCIL(p.160)を解いてみる。

SHARPEN YOUR PENCIL(p.160)

コード(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],
    highScore,
    bestSolutions,
    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;
    },
    getBestResults = function (scores, highScore) {
        var bestSolutions = [],
            i,
            max;
        for (i = 0, max = scores.length; i < max; i += 1) {
            if (scores[i] === highScore) {
                bestSolutions.push(i);
            }
        }
        return bestSolutions;
    };

highScore = printAndGetHighScores(scores);
print('Bubbles tests: ' + scores.length);
print('Highest bubble score: ' + highScore);

bestSolutions = getBestResults(scores, highScore);
print('Solutions with the highest score: ' + bestSolutions);










						

0 コメント:

コメントを投稿