開発環境
- 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 3(Introducing Functions: Getting functional)、EXERCISE(p.110)を解いてみる。
EXERCISE(p.110)
コード(BBEdit, Emacs)
var result = '',
n,
clunkCounter = 0,
clunk = function (times) {
var num = times;
while (num > 0) {
display("clunk");
num = num - 1;
}
},
thingamajig = function (size) {
var facky = 1;
if (size === 0) {
display("clank");
} else if (size === 1) {
display("thunk");
} else {
while (size > 1) {
facky = facky * size;
size = size - 1;
}
clunk(facky);
}
},
display = function (output) {
result += output + '\n';
clunkCounter += 1;
};
n = prompt('m = ');
n = parseInt(n, 10);
// 0 clank 1
// 1 thunk 2
// 2 clunk clunk 2
// 3 clunk clunk clunk clunk clunk clunk 6
// 4 clunk … clunk 24
// 5 clunk … clunk 120
thingamajig(n);
result += clunkCounter + '\n';
print(result);
0 コメント:
コメントを投稿