2017年7月14日金曜日

開発環境

Head First Python (Paul Barry (著)、O'Reilly Media)のChapter 2.(List Data: Working with Ordered Data)、Reviewing Our Experiments の SHARPEN YOUR PENCIL(No. 1567) を JavaScript で取り組んでみる。

SHARPEN YOUR PENCIL(No. 1567)

コード(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 phrase = "Don't panic!",
        plist = phrase.split('');

    p(phrase);
    p(plist);

    range(0, 4).forEach(() => plist.pop());
    plist.shift();
    plist.splice(plist.indexOf("'"), 1);
    plist = plist.concat([plist.pop(), plist.pop()]);
    plist.splice(2, 0, plist[3]);
    delete plist[4];
    
    let newPhrase = plist.join('');
    p(plist);
    p(newPhrase);
};

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

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

output();











 
						

0 コメント:

コメントを投稿

Comments on Google+: