2017年5月18日木曜日

開発環境

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 10(Hashes)の Exercise: A Hash Is a Mapping.を JavaScript で取り組んでみる。

Exercise: A Hash Is a Mapping.

コード(Emacs)

HTML5

<pre id="output0"></pre>

<button id="run0">run</button>
<button id="clear0">clear</button>

<script src="sample01.js"></script>

JavaScript

let btn0 = document.querySelector('#run0'),
    btn1 = document.querySelector('#clear0'),
    pre0 = document.querySelector('#output0'),
    input_file0 = document.querySelector('#file0'),
    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 employees = {Liz: 3000,
                 Bob: 2500,
                 Jack: 2000,
                 Betty: 1800};

let output = () => {
    p('Object.keys');
    Object.keys(employees)
        .forEach((key) => p(`${key}: ${employees[key]}`));

    p('for/in');
    for (let key in employees) {
        p(`${key}: ${employees[key]}`);
    }
};

let clear = () => pre0.textContent = '';

btn0.onclick = output;
btn1.onclick = clear;

output();











 
						

0 コメント:

コメントを投稿

Comments on Google+: