2017年4月26日水曜日

開発環境

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 4(Fruitful Subroutines)のEIncremental Development の Exercises.を JavaScript で取り組んでみる。

Exercises(Compare).

コード(Emacs)

HTML5

<pre id="output0"></pre>
<button id="run0">run</button>
<button id="clear0">clear</button>

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

JavaScript

let btn0 = document.querySelector('#run0'),
    btn1 = document.querySelector('#clear0'),
    pre0 = document.querySelector('#output0'),
    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 hypotenuse = (x, y) => 0.0;

let hypotenuse1 = (x, y) => {
    let xSquared = Math.pow(x, 2),
        ySquared = Math.pow(y, 2);

    p(`xSquared is ${xSquared}`);
    p(`ySquared is ${ySquared}`);

    return 0.0;
};

let hypotenuse2 = (x, y) => {
    let xSquared = Math.pow(x, 2),
        ySquared = Math.pow(y, 2),
        sum = xSquared + ySquared;

    p(`sum is ${sum}`);
    return 0.0;
};

let hypotenuse3 = (x, y) => {
    let xSquared = Math.pow(x, 2),
        ySquared = Math.pow(y, 2),
        sum = xSquared + ySquared,
        result = Math.sqrt(sum);

    return result;
};

let output = () => {
    p(hypotenuse(1, 1));
    p(hypotenuse(3, 4));
    p(hypotenuse(1, Math.sqrt(3)));

    p(hypotenuse1(1, 1));
    p(hypotenuse1(3, 4));
    p(hypotenuse1(1, Math.sqrt(3)));

    p(hypotenuse2(1, 1));
    p(hypotenuse2(3, 4));
    p(hypotenuse2(1, Math.sqrt(3)));

    p(hypotenuse3(1, 1));
    p(hypotenuse3(3, 4));
    p(hypotenuse3(1, Math.sqrt(3)));
};
    
btn0.onclick = output;
btn1.onclick = () => pre0.textContent = '';

output();













						

0 コメント:

コメントを投稿