2017年4月5日水曜日

開発環境

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 5(Fruitful subroutines)の Boolean functions の Exercise 5-4.を JavaScript で取り組 んでみる。

Exercise 5-4.

コード(Emacs)

HTML5

a = <input id="a0" type="number" min="1" step="1" value="1024">,
b = <input id="b0" type="number" min="1" step="1" value="2">
: <span id="output0"></span>

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

JavaScript

let input_a = document.querySelector('#a0'),
    input_b = document.querySelector('#b0'),
    inputs = [input_a, input_b],
    output = document.querySelector('#output0');

let isPowerOf = (a, b) => {
    return a === 1 ? true :
        b === 1 ? false :
        a === b ? true :
        a % b === 0 && isPowerOf(a / b, b);
};

let calc = () => {
    let a = parseInt(input_a.value, 10),
        b = parseInt(input_b.value, 10);

    output.textContent = isPowerOf(a, b);
};

inputs.forEach((input) => {    
    input.onchange = calc;
});

calc();
a = , b = :

0 コメント:

コメントを投稿

Comments on Google+: