2016年9月18日日曜日

開発環境

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

37(Password Generator)

コード(Emacs)

<label for="length0">
  What's the minimum length?
</label>
<input id="length0" type="number" min="1" step="1" value="8">
<br>
<label for="specials0">
  How many special characters? 
</label>
<input id="specials0" type="number" min="1" step="1" value="2">
<br>
<label for="numbers0">
  How many numbers? 
</label>
<input id="numbers0" type="number" min="1" step="1" value="2">
<br>
<div id="output0"></div>

<script src="array.js"></script>
<script src="sample37.js"></script>
(function () {
    'use strict';
    var characters,
        specials,
        numbers,
        input_length = document.querySelector('#length0'),
        input_specials = document.querySelector('#specials0'),
        input_numbers = document.querySelector('#numbers0'),
        inputs = [input_length, input_specials, input_numbers],
        div_output = document.querySelector('#output0'),
        nl = '<br>',
        generate,
        output;

    characters = Array.range(33, 127)
        .map(function (n) {
            return String.fromCharCode(n);
        });
    specials = Array.range(33, 48)
        .concat(Array.range(58, 65))
        .concat(Array.range(91, 97))
        .concat(Array.range(123, 127))
        .map(function (n) {
            return String.fromCharCode(n);
        });
    numbers = Array.range(10);

    generate = function () {
        var password,
            len = parseInt(input_length.value, 10),
            specials_len = parseInt(input_specials.value, 10),
            nums_len = parseInt(input_numbers.value, 10);

        len = Math.floor(len * (1 + Math.random()));
        password = Array.range(specials_len)
            .map(function () {
                return specials.random();
            })
            .concat(Array.range(nums_len)
                    .map(function () {
                        return numbers.random();
                    }))
            .concat(Array.range(len - specials_len - nums_len)
                    .map(function () {
                        return characters.random();
                    }))
            .shuffled()
            .join('');
        
        return password;
    };

    output = function () {
        var password = generate();

        div_output.innerHTML = 'Your pass word is <b>' + password + '<b>';
    };

    inputs.forEach(function (input) {
        input.onchange = output;
    });

    output();
}());



0 コメント:

コメントを投稿