開発環境
- 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 9(Arrays and Lists)の Exercise: Mapping and Filtering the Elements of a List.を JavaScript で取り組んでみる。
Exercise: Other Ways to Modify an Array.
コード(Emacs)
HTML5
<pre id="output0"></pre> <label for="n0">n = </label> <input id="n0" type="number" step="1" value="100"> <button id="run0">run</button> <button id="clear0">clear</button> <script src="sample_square.js"></script>
JavaScript
let btn0 = document.querySelector('#run0'), btn1 = document.querySelector('#clear0'), pre0 = document.querySelector('#output0'), input_n = document.querySelector('#n0'), p = (x) => pre0.textContent += x + '\n', range = (start, end, step=1) => { let result = []; for (let i = start; i < end; i += 1) { result.push(i); } return result; }; let output = () => { let input = range(1, 11), n = parseInt(input_n.value, 10); input.unshift(n); p(input.map((x) => x * x)); p(input.filter((x) => Math.sqrt(x) * Math.sqrt(x) === x)); }; input_n.onchange = output; btn0.onclick = output; btn1.onclick = () => pre0.textContent = ''; output();
0 コメント:
コメントを投稿