2017年4月30日日曜日

開発環境

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)、String Traversal with a while or for Loop の Exercise: String Traversal.を JavaScript で取り組んでみる。

Exercise: String Traversal.

コード(Emacs)

HTML5

<pre id="output0"></pre>
<label for="s0">string: </label>
<input id="s0" type="text" min="0" step="1" value="JavaScript">
<button id="run0">run</button>
<button id="clear0">clear</button>

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

JavaScript

let btn0 = document.querySelector('#run0'),
    btn1 = document.querySelector('#clear0'),
    pre0 = document.querySelector('#output0'),
    input_s = document.querySelector('#s0'),
    p = (x) => pre0.textContent += x + '\n',
    range = (start, end, step=1) => {
        let result = [];
        for (let i = start; i < end; i += step) {
            result.push(i);
        }
        return result;
    };

let backward = (word) => {
    for (let i = word.length; i > 0; i -= 1) {
        let letter = word.substring(i - 1, i);
        p(letter);
    }
};

let output = () => {
    ['', 'a', 'banana', '日本語', input_s.value]
        .forEach((word) => {
            p(`backward(${word})`);
            backward(word);
        });
};

input_s.onchange = output;
btn0.onclick = output;
btn1.onclick = () => pre0.textContent = '';

output();












 
						

0 コメント:

コメントを投稿

Comments on Google+: