開発環境
- 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 2(Writing Real Code: Going further)、EXERCISE(p.59)を解いてみる。
EXERCISE(p.59)
コード(BBEdit, Emacs)
var result = '',
location1 = 3,
location2 = 4,
location3 = 5,
guess,
hits = 0,
guesses = 0,
isSunk = false;
while (! isSunk ) {
guess = prompt('Ready aim, fire! (enter a number 0-6): ');
if (guess < 0 || guess > 6) {
result += 'Please enter a valid cell number!\n';
} else {
guesses += 1;
guess = parseInt(guess, 10);
if (guess === location1 || guess === location2 || guess === location3) {
hits += 1;
result += 'HIT!\n';
if (hits === 3) {
isSunk = true;
result += 'You sank my battleship!\n';
break;
}
} else {
result += 'MISS\n';
}
}
}
stats = 'You took ' + guesses + ' guesses to sink the battleship, ' +
'which means your hooting accuracy was ' + (3 / guesses);
result += stats;
print(result);
0 コメント:
コメントを投稿