2016年8月31日水曜日

開発環境

Exercises for Programmers: 57 Challenges to Develop Your Coding Skills (Brian P. Hogan 著、Pragmatic Bookshelf)のChapter 4(Making Decisions)、21(Numbers to Names)を取り組んでみる。

21(Numbers to Names)

コード(Emacs)

<label for="month_num0">
  Please enter the number of the month:
</label>
<input id="month_num0" type="number" min="1" max="12" step="1" value="3">
<br>
The name of the month is <span id="output0"></span>.
<br>
The name of the month is <span id="output1"></span>.

<script src="sample21.js"></script>
(function () {
    'use strict';
    var month_names,
        input_month_num = document.querySelector('#month_num0'),
        span_output0 = document.querySelector('#output0'),
        span_output1 = document.querySelector('#output1'),
        getMonthName,
        display;

    month_names = {
        1: 'January',
        2: 'Feburary',
        3: 'March',
        4: 'April',
        5: 'May',
        6: 'June',
        7: 'July',
        8: 'August',
        9: 'September',
        10: 'October',
        11: 'Novemer',
        12: 'December'
    };

    getMonthName = function (n) {
        var name = '';
        
        switch(n) {
        case 1:
            name = 'January';
            break;
        case 2:
            name = 'Feburary';
            break;
        case 3:
            name = 'March';
            break;
        case 4:
            name = 'April';
            break;
        case 5:
            name = 'May';
            break;
        case 6:
            name = 'June';
            break;
        case 7:
            name = 'July';
            break;
        case 8:
            name = 'August';
            break;
        case 9:
            name = 'September';
            break;
        case 10:
            name = 'October';
            break;
        case 11:
            name = 'Novemer';
            break;
        case 12:
            name = 'December';
            break;        
        }
        return name;
    };
    display = function () {
        var n = parseInt(input_month_num.value, 10);

        span_output0.innerText = month_names[n];
        span_output1.innerText = getMonthName(n);
    };

    input_month_num.onchange = display;
    input_month_num.onkeyup = display;

    display();
}());

The name of the month is .
The name of the month is .

0 コメント:

コメントを投稿