2017年4月10日月曜日

開発環境

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 7(Strings)の Boolean functions の Looping and counting の Exercise.を JavaScript で取り組 んでみる。

Looping and counting の Exercise.

コード(Emacs)

HTML5

word: <input id="word0" type="text" value="banana">
letter: <input id="letter0" type="text" value="a">
<br>
<button id="run0">run</button>
<button id="clear0">clear</button>
<pre id="output0"></pre>

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

JavaScript

let input0 = document.querySelector('#word0'),
    input1 = document.querySelector('#letter0'),
    btn0 = document.querySelector('#run0'),
    btn1 = document.querySelector('#clear0'),
    pre0 = document.querySelector('#output0');

let count = (word, letter) => {
    return word.split('').reduce(
        (prev, c) => prev + (c === letter ? 1 : 0),
        0);
};

let output = () => {
    let word = input0.value,
        letter = input1.value;

    pre0.textContent += count(word, letter) + '\n';
};
    

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

output();
word: letter:










 
						

0 コメント:

コメントを投稿

Comments on Google+: