2017年7月13日木曜日

開発環境

Head First Python (Paul Barry (著)、O'Reilly Media)のChapter 1.(The Basics: Getting Started Quickly)、Reviewing Our Experiments の Code Experiments Magnets(No. 1020) を JavaScript で取り組んでみる。

Code Experiments Magnets(No. 1020)

コード(Emacs)

HTML5

<pre id="output0">
</pre>

<button id="run0">run</button>
<button id="clear0">clear</button>

<script src="sample1.js"></script>
  

JavaScript

let pre0 = document.querySelector('#output0'),
    btn0 = document.querySelector('#run0'),
    btn1 = document.querySelector('#clear0'),
    range = (start, end, step=1) => {
        let res = [];

        for (let i = start; i < end; i += step) {
            res.push(i);
        }
        return res;
    },
    p = (x) => pre0.textContent += x + '\n';

let output = () => {
    let odds = range(1, 60).filter((n) => n % 2 == 1),
        n = 5;

    let loop = (i) => {
        let rightThisMinute = (new Date()).getMinutes();

        if (odds.indexOf(rightThisMinute) !== -1) {
            p('This minute seems a little odd.');
        } else {
            p('Not an odd minute.');
        }
        if (i < 4) {
            let waiteTime = Math.floor(Math.random()) * 60 + 1;
            setTimeout(() => loop(i + 1), waiteTime * 1000 * 60);
        }
    };
    loop(0);
};

let clear = () => pre0.textContent = '';

btn0.onclick = output;
btn1.onclick = clear;

output();














						

0 コメント:

コメントを投稿