2017年4月27日木曜日

開発環境

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

Exercises(Compare).

コード(Emacs)

HTML5

<pre id="output0"></pre>
x = <input id="x0" type="number" value="1">
y = <input id="y0" type="number" value="1">
z = <input id="z0" type="number" value="1">
<br>
<button id="run0">run</button>
<button id="clear0">clear</button>

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

JavaScript

let btn0 = document.querySelector('#run0'),
    btn1 = document.querySelector('#clear0'),
    pre0 = document.querySelector('#output0'),
    input_x = document.querySelector('#x0'),
    input_y = document.querySelector('#y0'),
    input_z = document.querySelector('#z0'),
    inputs = [input_x, input_y, input_z],
    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 isBetween = (x, y, z) => x <= y && y <= z;

let output = () => {
    range(1, 3)
        .forEach((x) =>
                 range(1, 3)
                 .forEach((y) =>
                          range(1, 3)
                          .forEach((z) =>
                                   p(`${x}, ${y}, ${z}: ${isBetween(x, y, z)}`)
                                  )
                         )
                );
    let [x, y, z] = inputs.map((input) => parseFloat(input.value));

    p(`${x}, ${y}, ${z}: ${isBetween(x, y, z)}`);
};

inputs.forEach((input) => input.onchange = output);
btn0.onclick = output;
btn1.onclick = () => pre0.textContent = '';

output();

x = 
y = 
z = 

0 コメント:

コメントを投稿