開発環境
- 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 7(Strings)の Boolean functions の String traversal with a for loop の Exercise.を JavaScript で取り組 んでみる。
String traversal with a for loop の Exercise.
コード(Emacs)
HTML5
letters: <input id="letters0" type="text" value="banana"> <button id="run0">run</button> <button id="clear0">clear</button> <pre id="output0"></pre> <script src="sample_for.js"></script>
JavaScript
let input0 = document.querySelector('#letters0'), btn0 = document.querySelector('#run0'), btn1 = document.querySelector('#clear0'), pre0 = document.querySelector('#output0'); let backward = (letters) => { let index = letters.length; while (index > 0) { index -= 1; let letter = letters.substr(index, 1); pre0.textContent += `${letter}\n`; } }; let output = () => { pre0.textContent += '1.\n'; let letters = input0.value; backward(letters); pre0.textContent += '2.\n'; let suffix = 'ack', jCode = 'J'.charCodeAt(0), qCode = 'Q'.charCodeAt(0); for (let code = jCode; code <= qCode; code += 1) { let letter = String.fromCharCode(code); if (letter === 'O' || letter === 'Q') { pre0.textContent += `${letter}u${suffix}\n` } else { pre0.textContent += `${letter}${suffix}\n` } } }; btn0.onclick = output; btn1.onclick = () => { pre0.textContent = ''; }; output();letters:
0 コメント:
コメントを投稿