開発環境
- OS X El Capitan - Apple (OS)
- Emacs (Text Editor)
- JavaScript (プログラミング言語)
- kjs-array (JavaScript Library)
- Safari(Web browser)
- JavaScript 第6版 (David Flanagan(著)、村上 列(翻訳)、オライリージャパン)(参考書籍)
- JavaScriptリファレンス 第6版(David Flanagan(著)、木下 哲也(翻訳)、オライリージャパン)(参考書籍)
Exercises for Programmers: 57 Challenges to Develop Your Coding Skills (Brian P. Hogan 著、Pragmatic Bookshelf)のChapter 7(Data Structures)、33(Magic 8 Ball)を取り組んでみる。
33(Magic 8 Ball)
コード(Emacs)
<div id="output0"></div>
<button id="play0">Play!</button>
<script src="array.js"></script>
<script src="sample33.js"></script>
(function () {
'use strict';
var balls = ['Will I be rich and famous?', 'ball2', 'ball3', 'ball4',
'ball5', 'ball6', 'ball7', 'ball8'],
answers = ['Yes.', 'No.', 'Maybe.', 'Ask again later.'],
div_output = document.querySelector('#output0'),
button_play = document.querySelector('#play0'),
nl = '<br>',
output;
output = function () {
balls.shuffle();
div_output.innerHTML = '';
balls.forEach(function (ball) {
div_output.innerHTML +=
"What's your question? " + ball + nl +
answers.random() + nl;
});
};
button_play.onclick = output;
output();
}())
What's your question? ball3
Maybe.
What's your question? ball4
Maybe.
What's your question? ball2
No.
What's your question? ball5
Yes.
What's your question? ball8
Ask again later.
What's your question? Will I be rich and famous?
Maybe.
What's your question? ball7
No.
What's your question? ball6
Maybe.
Maybe.
What's your question? ball4
Maybe.
What's your question? ball2
No.
What's your question? ball5
Yes.
What's your question? ball8
Ask again later.
What's your question? Will I be rich and famous?
Maybe.
What's your question? ball7
No.
What's your question? ball6
Maybe.
0 コメント:
コメントを投稿