2017年4月22日土曜日

開発環境

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(Conditionals and Recursion)のExercises 4-1、2、3.を JavaScript で取り組んでみる。

Exercises 4-1、2、3.

コード(Emacs)

HTML5

<pre id="output0"></pre>
<ol>
  <li>
    seconds: <input id="seconds0" type="number" step="1" value="240000">
  </li>
  <li>
    a: <input id="a0" type="number" min="0" step="1" value="1">
    b: <input id="b0" type="number" min="0" step="1" value="2">
    c: <input id="c0" type="number" min="0" step="1" value="3">
    n: <input id="n0" type="number" min="3" step="1" value="4">
  </li>
  <li>
    a: <input id="a1" type="number" step="1" value="1">
    b: <input id="b1" type="number" step="1" value="2">
    c: <input id="c1" type="number" step="1" value="3">
  </li>
</ol>
<button id="run0">run</button>
<button id="clear0">clear</button>

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

JavaScript

let input0 = document.querySelector('#seconds0'),
    input_a0 = document.querySelector('#a0'),
    input_b0 = document.querySelector('#b0'),
    input_c0 = document.querySelector('#c0'),
    input_n0 = document.querySelector('#n0'),
    input_a1 = document.querySelector('#a1'),
    input_b1 = document.querySelector('#b1'),
    input_c1 = document.querySelector('#c1'),
    btn0 = document.querySelector('#run0'),
    btn1 = document.querySelector('#clear0'),
    pre0 = document.querySelector('#output0'),
    p = (x) => pre0.textContent += x + '\n';

let daysHoursMinutesSeconds = (seconds) => {
    let days = Math.floor(seconds / (24 * 60 * 60));
    seconds %= (24 * 60 * 60)
    let hours = Math.floor(seconds / (60 * 60));
    seconds %= (60 * 60)
    let minutes = Math.floor(seconds / 60);
    seconds %= 60;
    p(`days: ${days}, hours: ${hours}, minutes: ${minutes}, seconds: ${seconds}`);
};
let daysHoursMinutesSecondsInput = () => {
    let seconds = parseInt(input0.value, 10);

    daysHoursMinutesSeconds(seconds);
};

let checkFermat = (a, b, c, n) => {
    if ((Math.pow(a, n) + Math.pow(b, n) === Math.pow(c, n)) && n > 2) {
        p('Holy smokes, Fermat was wrong!');
    } else {
        p("No, that doesn't work.");
    }
};
let checkFermatInput = () => {
    let a = parseInt(input_a0.value, 10),
        b = parseInt(input_b0.value, 10),
        c = parseInt(input_c0.value, 10),
        n = parseInt(input_n0.value, 10);

    checkFermat(a, b, c, n);
};

let isTriangle = (a, b, c) => {
    if (a >= b + c || b >= c + a || c >= a + b) {
        p('No');
    } else {
        p('Yes');
    }
};

let isTriangleInput = () => {
    let a = parseInt(input_a1.value, 10),
        b = parseInt(input_b1.value, 10),
        c = parseInt(input_c1.value, 10);

    isTriangle(a, b, c);
};

let output = () => {
    p('1-2.');
    daysHoursMinutesSeconds(240000);

    p('1-3.');
    daysHoursMinutesSecondsInput();

    p('2-2.');
    checkFermatInput();

    p('3-2.');
    isTriangleInput();
};
    

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

output();

  1. seconds:
  2. a: b: c: n:
  3. a: b: c:

0 コメント:

コメントを投稿