2016年9月21日水曜日

開発環境

Exercises for Programmers: 57 Challenges to Develop Your Coding Skills (Brian P. Hogan 著、Pragmatic Bookshelf)のChapter 7(Data Structures)、39(Sorting Records)を取り組んでみる。

39(Sorting Records)

コード(Emacs)

HTML5

<label for="sort0">sort by: </label>
<select id="sort0">
  <option value="first">First Name</option>
  <option value="last">Last Name</option>
  <option value="pos">Position</option>
  <option value="sep">Separation date</option>  
</select>
<br>
<div id="output0"></div>

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

JavaScript

(function () {
    'use strict';
    var dataset,
        first_names,
        header = ['Name', 'Position', 'Separation Date'],
        input_sort = document.querySelector('#sort0'),
        div_output = document.querySelector('#output0'),
        output;

    dataset = {
        John: {
            'Last Name': 'Johnson',
            'Position': 'Manager',
            'Separation date': new Date(2016, 11, 31),
        },
        Tou: {
            'Last Name': 'Xialong',
            'Position': 'Software Engineer',
            'Separation date': new Date(2016, 9, 5),
        },
        Michaela: {
            'Last Name': 'Michaleson',
            'Position': 'District Manager',
            'Separation date': new Date(2015, 11, 19),
        },
        Jake: {
            'Last Name': 'Jacobson',
            'Position': 'Programmer',
        },
        Jacquelyn: {
            'Last Name': 'Jackson',
            'Position': 'DBA',
        },
        Sally: {
            'Last Name': 'Weber',
            'Position': 'Web Developer',
            'Separation date': new Date(2015, 11, 18),
        },
    };
    first_names = Object.keys(dataset);
    output = function () {
        var sort_by = input_sort.value,
            result = '';

        if (sort_by === 'first') {
            first_names.sort();
        } else if (sort_by === 'last') {
            first_names.sort(function (x, y) {
                return dataset[x]['Last Name'] <= dataset[y]['Last Name'] ?
                    1 : -1;
            });
        } else if (sort_by === 'pos') {
            first_names.sort(function (x, y) {
                return dataset[x]['Position'] < dataset[y]['Position'] ?
                    1 : -1;
            });
        } else if (sort_by === 'sep') {
            first_names.sort(function (x, y) {
                var x = dataset[x]['Separation date'],
                    y = dataset[y]['Separation date'];
                
                if (x === undefined) {
                    return -1;
                }
                if ( y === undefined) {
                    return 1;
                }
                return x.getTime() - y.getTime();
            });
        }
        result += '<table border="1">'
        result += '<tr><th>' + header.join('</th><th>') + '</th></tr>';
        result += '<tr>';
        result += first_names.map(function (first) {
            var employee = dataset[first];
            return '<td>' +
                [
                    first + ' ' + employee['Last Name'],
                    employee['Position'],
                    employee['Separation date'] === undefined ? '':
                        employee['Separation date']
                        .toISOString()
                        .slice(0, 10)
                ]
                .join('</td><td>') + '</td>';

        }).join('</tr><tr>');
        result += '</tr>';
        result += '</table>';

        div_output.innerHTML = result;
    };
    input_sort.onchange = output;
    output();

}());

0 コメント:

コメントを投稿

Comments on Google+: