2017年6月3日土曜日

開発環境

Head First JavaScript Programming (Eric T. Freeman (著)、Elisabeth Robson (著)、O'Reilly Media)の Chapter 13.(Extra strength objects - Using Prototypes)の EXERCISE(No. 8943)を取り組んでみる。

EXERCISE(No. 8943)

コード(Emacs)

HTML5

<pre id="output0"></pre>
<input id="text0" type="text" value="javascript">
<button id="run0">run</button>
<button id="clear0">clear</button>
<script src="sample7.js"></script>

JavaScript

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

String.prototype.isPalindrome = function () {
    return this.valueOf() === this.split('').reverse().join('');
};
let output = () => {
    let s = input_text.value;

    ['', 'a', 'ab', 'aba', 'abc', s]
        .forEach((s) => p(`${s}: ${s.isPalindrome()}`));
};

let clear = () => pre0.textContent = '';

input_text.onchange = output;

btn0.onclick = output;
btn1.onclick = clear;

output();






    







						

0 コメント:

コメントを投稿

Comments on Google+: