2017年4月15日土曜日

開発環境

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

Exercises 7-3.

コード(Emacs)

HTML5

word: <input id="word0" type="text" value="IBM">
n = <input id="n0" type="number" step="1" value="-1">
<br>
<button id="run0">run</button>
<button id="clear0">clear</button>
<pre id="output0"></pre>

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

JavaScript

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

let rotateWord = (word, n) => {
    let m = n % 26,
        acode = 'A'.charCodeAt(0),
        zcode = 'Z'.charCodeAt(0);
        
    return word.split('')
        .map((c) => c.charCodeAt(0))
        .map((n) => {
            let k = n + m;
            return k < acode ? zcode - (acode - k) + 1 :
                zcode < k ? acode + k - zcode - 1 :
                k;
        })
        .map((code) => String.fromCharCode(code))
        .join('');
};

let output = () => {
    let word = input0.value,
        n = parseInt(input1.value, 10);

    p(rotateWord(word, n));
};
    

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

output();
word: n =










 
						

0 コメント:

コメントを投稿

Comments on Google+: