2017年4月16日日曜日

開発環境

Think Perl 6: How to Think Like a Computer Scientist (Laurent Rosenfeld(著)、Allen B. Downey(著)、Oreilly & Associates Inc)のPart 1(Starting with the basics)、Chapter 8(Case study: word play)の Boolean functions の Exercises 8-1.を JavaScript で取り組んでみる。

Exercises 8-1.

コード(Emacs)

HTML5

<input id="file0" type="file">
<br>
<button id="run0">run</button>
<button id="clear0">clear</button>
<pre id="output0"></pre>

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

JavaScript

let input0 = document.querySelector('#file0'),
    btn0 = document.querySelector('#run0'),
    btn1 = document.querySelector('#clear0'),
    pre0 = document.querySelector('#output0'),
    p = (x) => pre0.textContent += x + '\n';

let output = () => {
    let reader = new FileReader();

    reader.onload = () => {
        reader.result.split('\n')
            .map((word) => word.trim())
            .forEach((word) => {
                if (word.length > 20) {
                    p(word);
                }
            })
    };

    reader.readAsText(input0.files[0]);
};
    

btn0.onclick = output;
btn1.onclick = () => {
    pre0.textContent = '';
};

output();














						

0 コメント:

コメントを投稿