2017年4月17日月曜日

開発環境

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-2.を JavaScript で取り組んでみる。

Exercises 8-2.

コード(Emacs)

HTML5

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

<script src="sample2.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 hasNoE = (word) => word.indexOf('e') === -1;

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

    reader.onload = () => {
        let words = 
            reader.result.split('\n')
            .map((word) => word.trim());

        p(`${words.filter(hasNoE).length / words.length * 100}%`);
    };

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

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

output();














						

0 コメント:

コメントを投稿