2016年9月14日水曜日

開発環境

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();
}())

0 コメント:

コメントを投稿

Comments on Google+: