開発環境
- 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)、String Traversal with a while or for Loop の Exercise: The Ducklings.を JavaScript で取り組んでみる。
Exercise: The Ducklings.
コード(Emacs)
HTML5
<pre id="output0"></pre> <button id="run0">run</button> <button id="clear0">clear</button> <script src="sample_duckl.js"></script>
JavaScript
let btn0 = document.querySelector('#run0'),
btn1 = document.querySelector('#clear0'),
pre0 = document.querySelector('#output0'),
p = (x) => pre0.textContent += x + '\n',
range = (start, end, step=1) => {
let result = [];
for (let i = start; i < end; i += step) {
result.push(i);
}
return result;
};
let output = () => {
let suffix = 'ack';
p(
range('J'.codePointAt(0), 'Q'.codePointAt('Q') + 1)
.map((code) => {
let c = String.fromCodePoint(code);
if (c === 'O' || c === 'Q') {
return `${c}u${suffix}`
}
return `${c}${suffix}`;
})
.join('\n')
);
};
btn0.onclick = output;
btn1.onclick = () => pre0.textContent = '';
output();
0 コメント:
コメントを投稿