2014年9月14日日曜日

開発環境

Head First JavaScript Programming (Eric T. Freeman (著)、 Elisabeth Robson (著)、 O'Reilly Media )のChapter 10(Liberated functions: First Class Functions)、EXERCISE(p.460)を解いてみる。

EXERCISE(p.460)

コード(BBEdit, Emacs)

var compareNumbersDesc = function (num1, num2) {
        if (num2 > num1) {
            return 1;
        }
        if (num1 === num2) {
            return 0;
        }
        return -1;
    },
    nums = [1, 2, 3, 4, 5],
    i,
    j,
    max,
    x,
    y,
    output = '';

for (i = 0, max = nums.length; i < max; i += 1) {
    x = nums[i];
    for (j = 0; j < max; j += 1) {
        y = nums[j];
        output += x  + ' ' + y + ' ' + compareNumbersDesc(x, y) + '\n';
    }
}

nums.sort(compareNumbersDesc);

output += nums

print(output);










						

0 コメント:

コメントを投稿