2016年11月7日月曜日

開発環境

Eloquent JavaScript(Marijn Haverbeke 著、No Starch Press)のPart 1(Language)、Chapter 10(Modules)、Exercises(Month Names)を取り組んでみる。

Exercises(Month Names)

コード(Emacs)

JavaScript

'use strict';
let monthNames = (() => {
    let names = ['January', 'February', 'March', 'April', 'May', 'June', 'July',
                 'August', 'September', 'October', 'November', 'December'];

    return {
        name(number) {
            return names[number];
        },
        number(name) {
            return names.indexOf(name);
        },
    };
})();

let names = [];
for (let i = 0; i < 12; i += 1) {
    let name = monthNames.name(i);
    console.log(`${i}: ${name}`);
    names.push(name);
}
names.forEach((name) => console.log(`${name}: ${monthNames.number(name)}`));

入出力結果(Terminal, Node.js)

$ node sample1.js
0: January
1: February
2: March
3: April
4: May
5: June
6: July
7: August
8: September
9: October
10: November
11: December
January: 0
February: 1
March: 2
April: 3
May: 4
June: 5
July: 6
August: 7
September: 8
October: 9
November: 10
December: 11
$

0 コメント:

コメントを投稿

Comments on Google+: