開発環境
- macOS Sierra - Apple (OS)
- Emacs (Text Editor)
- JavaScript (プログラミング言語)
- Node.js, Safari(JavaScript エンジン)
- Learning JavaScript [邦訳](参考書籍)
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-5.を JavaScript で取り組 んでみる。
Exercise 5-5.
コード(Emacs)
HTML5
<p>gcd( <input id="a0" type="number" min="1" step="1" value="12345">, <input id="b0" type="number" min="1" step="1" value="67890"> ) = <span id="output0"></span> </p> <script src="sample5.js"></script>
JavaScript
let input_a = document.querySelector('#a0'),
input_b = document.querySelector('#b0'),
inputs = [input_a, input_b],
span0 = document.querySelector('#output0');
let gcd = (a, b) => {
return b === 0 ? a : gcd(b, a % b);
}
let output = () => {
let a = parseInt(input_a.value, 10),
b = parseInt(input_b.value, 10);
span0.textContent = gcd(a, b);
};
inputs.forEach((input) => {
input.onchange = output;
});
output();
gcd( , ) =
0 コメント:
コメントを投稿