開発環境
- OS X El Capitan - Apple (OS)
- Emacs (Text Editor)
- JavaScript (プログラミング言語)
- jQuery (Library)
- Safari(Web browser)
Eloquent JavaScript(Marijn Haverbeke 著、No Starch Press)のPart 1(Language)、Chapter 3(Functions)、Exercises(Minimum, Recursion, Bean Counting)を取り組んでみる。
Exercises
コード(Emacs)
(function () {
'use strict';
var nums = [],
i,
min,
str = '',
ch,
isEven,
countChar,
countBs;
for (i = -9; i < 10; i += 1) {
nums.push(i);
}
for (i = 0; i < 100; i += 1) {
str += String.fromCharCode(Math.floor(Math.random() * 26) + 65);
}
min = function (x, y) {
return x < y ? x : y;
};
nums.forEach(function (x) {
nums.forEach(function (y) {
console.log(x + ', ' + y + ': ' + min(x, y));
});
});
isEven = function (x) {
if (x < 0) {
return 'Invliad number';
}
if (x === 0) {
return true;
}
if (x === 1) {
return false;
}
return isEven(x - 2);
};
console.log('isEven');
nums.forEach(function (x) {
console.log(x + ': ' + isEven(x));
});
console.log('57: ' + isEven(57));
console.log('75: ' + isEven(75));
console.log('-1: ' + isEven(-1));
countBs = function (s) {
var i,
len = s.length,
count = 0;
for (i = 0; i < len; i += 1) {
if (s.charAt(i) === 'B') {
count += 1;
}
}
return count;
};
console.log('str: ' + str);
console.log(countBs(str));
countChar = function (s, ch) {
var i,
len = s.length,
count = 0;
for (i = 0; i < len; i += 1) {
if (s.charAt(i) === ch) {
count += 1;
}
}
return count;
};
ch = String.fromCharCode(Math.floor(Math.random() * 26) + 65);
console.log(ch, countChar(str, ch));
}());
入出力結果(Terminal, Node.js)
$ node sample.js -9, -9: -9 -9, -8: -9 -9, -7: -9 -9, -6: -9 -9, -5: -9 -9, -4: -9 -9, -3: -9 -9, -2: -9 -9, -1: -9 -9, 0: -9 -9, 1: -9 -9, 2: -9 -9, 3: -9 -9, 4: -9 -9, 5: -9 -9, 6: -9 -9, 7: -9 -9, 8: -9 -9, 9: -9 -8, -9: -9 -8, -8: -8 -8, -7: -8 -8, -6: -8 -8, -5: -8 -8, -4: -8 -8, -3: -8 -8, -2: -8 -8, -1: -8 -8, 0: -8 -8, 1: -8 -8, 2: -8 -8, 3: -8 -8, 4: -8 -8, 5: -8 -8, 6: -8 -8, 7: -8 -8, 8: -8 -8, 9: -8 -7, -9: -9 -7, -8: -8 -7, -7: -7 -7, -6: -7 -7, -5: -7 -7, -4: -7 -7, -3: -7 -7, -2: -7 -7, -1: -7 -7, 0: -7 -7, 1: -7 -7, 2: -7 -7, 3: -7 -7, 4: -7 -7, 5: -7 -7, 6: -7 -7, 7: -7 -7, 8: -7 -7, 9: -7 -6, -9: -9 -6, -8: -8 -6, -7: -7 -6, -6: -6 -6, -5: -6 -6, -4: -6 -6, -3: -6 -6, -2: -6 -6, -1: -6 -6, 0: -6 -6, 1: -6 -6, 2: -6 -6, 3: -6 -6, 4: -6 -6, 5: -6 -6, 6: -6 -6, 7: -6 -6, 8: -6 -6, 9: -6 -5, -9: -9 -5, -8: -8 -5, -7: -7 -5, -6: -6 -5, -5: -5 -5, -4: -5 -5, -3: -5 -5, -2: -5 -5, -1: -5 -5, 0: -5 -5, 1: -5 -5, 2: -5 -5, 3: -5 -5, 4: -5 -5, 5: -5 -5, 6: -5 -5, 7: -5 -5, 8: -5 -5, 9: -5 -4, -9: -9 -4, -8: -8 -4, -7: -7 -4, -6: -6 -4, -5: -5 -4, -4: -4 -4, -3: -4 -4, -2: -4 -4, -1: -4 -4, 0: -4 -4, 1: -4 -4, 2: -4 -4, 3: -4 -4, 4: -4 -4, 5: -4 -4, 6: -4 -4, 7: -4 -4, 8: -4 -4, 9: -4 -3, -9: -9 -3, -8: -8 -3, -7: -7 -3, -6: -6 -3, -5: -5 -3, -4: -4 -3, -3: -3 -3, -2: -3 -3, -1: -3 -3, 0: -3 -3, 1: -3 -3, 2: -3 -3, 3: -3 -3, 4: -3 -3, 5: -3 -3, 6: -3 -3, 7: -3 -3, 8: -3 -3, 9: -3 -2, -9: -9 -2, -8: -8 -2, -7: -7 -2, -6: -6 -2, -5: -5 -2, -4: -4 -2, -3: -3 -2, -2: -2 -2, -1: -2 -2, 0: -2 -2, 1: -2 -2, 2: -2 -2, 3: -2 -2, 4: -2 -2, 5: -2 -2, 6: -2 -2, 7: -2 -2, 8: -2 -2, 9: -2 -1, -9: -9 -1, -8: -8 -1, -7: -7 -1, -6: -6 -1, -5: -5 -1, -4: -4 -1, -3: -3 -1, -2: -2 -1, -1: -1 -1, 0: -1 -1, 1: -1 -1, 2: -1 -1, 3: -1 -1, 4: -1 -1, 5: -1 -1, 6: -1 -1, 7: -1 -1, 8: -1 -1, 9: -1 0, -9: -9 0, -8: -8 0, -7: -7 0, -6: -6 0, -5: -5 0, -4: -4 0, -3: -3 0, -2: -2 0, -1: -1 0, 0: 0 0, 1: 0 0, 2: 0 0, 3: 0 0, 4: 0 0, 5: 0 0, 6: 0 0, 7: 0 0, 8: 0 0, 9: 0 1, -9: -9 1, -8: -8 1, -7: -7 1, -6: -6 1, -5: -5 1, -4: -4 1, -3: -3 1, -2: -2 1, -1: -1 1, 0: 0 1, 1: 1 1, 2: 1 1, 3: 1 1, 4: 1 1, 5: 1 1, 6: 1 1, 7: 1 1, 8: 1 1, 9: 1 2, -9: -9 2, -8: -8 2, -7: -7 2, -6: -6 2, -5: -5 2, -4: -4 2, -3: -3 2, -2: -2 2, -1: -1 2, 0: 0 2, 1: 1 2, 2: 2 2, 3: 2 2, 4: 2 2, 5: 2 2, 6: 2 2, 7: 2 2, 8: 2 2, 9: 2 3, -9: -9 3, -8: -8 3, -7: -7 3, -6: -6 3, -5: -5 3, -4: -4 3, -3: -3 3, -2: -2 3, -1: -1 3, 0: 0 3, 1: 1 3, 2: 2 3, 3: 3 3, 4: 3 3, 5: 3 3, 6: 3 3, 7: 3 3, 8: 3 3, 9: 3 4, -9: -9 4, -8: -8 4, -7: -7 4, -6: -6 4, -5: -5 4, -4: -4 4, -3: -3 4, -2: -2 4, -1: -1 4, 0: 0 4, 1: 1 4, 2: 2 4, 3: 3 4, 4: 4 4, 5: 4 4, 6: 4 4, 7: 4 4, 8: 4 4, 9: 4 5, -9: -9 5, -8: -8 5, -7: -7 5, -6: -6 5, -5: -5 5, -4: -4 5, -3: -3 5, -2: -2 5, -1: -1 5, 0: 0 5, 1: 1 5, 2: 2 5, 3: 3 5, 4: 4 5, 5: 5 5, 6: 5 5, 7: 5 5, 8: 5 5, 9: 5 6, -9: -9 6, -8: -8 6, -7: -7 6, -6: -6 6, -5: -5 6, -4: -4 6, -3: -3 6, -2: -2 6, -1: -1 6, 0: 0 6, 1: 1 6, 2: 2 6, 3: 3 6, 4: 4 6, 5: 5 6, 6: 6 6, 7: 6 6, 8: 6 6, 9: 6 7, -9: -9 7, -8: -8 7, -7: -7 7, -6: -6 7, -5: -5 7, -4: -4 7, -3: -3 7, -2: -2 7, -1: -1 7, 0: 0 7, 1: 1 7, 2: 2 7, 3: 3 7, 4: 4 7, 5: 5 7, 6: 6 7, 7: 7 7, 8: 7 7, 9: 7 8, -9: -9 8, -8: -8 8, -7: -7 8, -6: -6 8, -5: -5 8, -4: -4 8, -3: -3 8, -2: -2 8, -1: -1 8, 0: 0 8, 1: 1 8, 2: 2 8, 3: 3 8, 4: 4 8, 5: 5 8, 6: 6 8, 7: 7 8, 8: 8 8, 9: 8 9, -9: -9 9, -8: -8 9, -7: -7 9, -6: -6 9, -5: -5 9, -4: -4 9, -3: -3 9, -2: -2 9, -1: -1 9, 0: 0 9, 1: 1 9, 2: 2 9, 3: 3 9, 4: 4 9, 5: 5 9, 6: 6 9, 7: 7 9, 8: 8 9, 9: 9 isEven -9: Invliad number -8: Invliad number -7: Invliad number -6: Invliad number -5: Invliad number -4: Invliad number -3: Invliad number -2: Invliad number -1: Invliad number 0: true 1: false 2: true 3: false 4: true 5: false 6: true 7: false 8: true 9: false 57: false 75: false -1: Invliad number str: UNCQULWQPZTBZYLOPRPAZSIEVKQEKZMRVJSEUDICGJOWQKZPVQFNBNYJLATSBCBWPBRUFCBVVIEWLHXTPDLPWPRGRSKRJJXRCYJX 6 A 2 $
0 コメント:
コメントを投稿