## 2017年4月23日日曜日

### JavaScript - Starting with the basics - Conditionals and Recursion(for loop, Fibonacci numbers, keyboard input)

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-4.を JavaScript で取り組んでみる。

Exercises 4-4.

コード(Emacs)

HTML5

```<pre id="output0"></pre>
n = <input id="n0" type="number" min="1" step="1" value="20">
<br>
<button id="run0">run</button>
<button id="clear0">clear</button>

<script src="sample2.js"></script>
```

JavaScript

```let input0 = document.querySelector('#n0'),
btn0 = document.querySelector('#run0'),
btn1 = document.querySelector('#clear0'),
pre0 = document.querySelector('#output0'),
p = (x) => pre0.textContent += x + '\n';

let output = () => {
p('4-1.');
let a = 1,
b = 1;

p(1);
p(1);
for (let i = 3; i <= 20; i += 1) {
p(a + b);
[a, b] = [b, a + b];
}

p('4-2.');
let n = parseInt(input0.value, 10);
a = 1;
b = 1;
for (let i = 3; i <= n; i += 1) {
[a, b] = [b, a + b];
}
p(b);
};

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

output();
```

n =