2017年7月17日月曜日

開発環境

Head First Python (Paul Barry (著)、O'Reilly Media)のChapter 3.(Structured Data: Working with Structured Data)の SHARPEN YOUR PENCIL(No. 2621) を JavaScript で取り組んでみる。

SHARPEN YOUR PENCIL(No. 2621)

コード(Emacs)

HTML5

<pre id="output0"></pre>
<label for="word0">word: </label>
<input id="word0" type="text" value="JavaScript">
<button id="run0">run</button>
<button id="clear0">clear</button>

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

JavaScript

let pre0 = document.querySelector('#output0'),
    btn0 = document.querySelector('#run0'),
    btn1 = document.querySelector('#clear0'),
    input0 = document.querySelector('#word0'),
    inputs = [input0],
    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 vowels = ['a', 'e', 'i', 'o', 'u'];

let output = () => {
    let word = input0.value,
        found = {};

    word.split('').forEach((letter) => {
        if (vowels.indexOf(letter) !== -1) {
            found[letter] = 1;
        }
    });
    p(word);
    Object.keys(found)
        .sort()
        .forEach((vowel) => p(vowel));
};

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

btn0.onclick = output;
btn1.onclick = clear;
inputs.forEach((input) => input.onchange = output);

output();
















						

0 コメント:

コメントを投稿