Tip: Click lines to highlight, hold ctrl/cmd to multi-select

Password Generator (14-Apr @ 11:48)

Generates a pronounceable password.

remy

Syntax Highlighted Code

  1. function generatePassword(limit, inclNumbers) {
  2.     var vowels = 'aeiou'.split('');
  3.     var constonants = 'bcdfghjklmnpqrstvwxyz'.split('');
  4.     var word = '', i, num;
  5.  
  6.     if (!limit) limit = 8;
  7.  
  8.     for (i = 0; i < (inclNumbers ? limit - 3 : limit); i++) {
  9.         if (i % 2 == 0) { // even = vowels
  10.             word += vowels[Math.floor(Math.random() * 4)];
  11.         } else {
  12.             word += constonants[Math.floor(Math.random() * 20)];
  13.         }
  14.     }
  15.  
  16.     if (inclNumbers) {
  17.         num = Math.floor(Math.random() * 99) + '';
  18.         if (num.length == 1) num = '00' + num;
  19.         else if (num.length == 2) num = '0' + num;
  20.         word += num;
  21.     }
  22.  
  23.     return word.substr(0, limit);
  24. }

Plain Code

function generatePassword(limit, inclNumbers) {
    var vowels = 'aeiou'.split('');
    var constonants = 'bcdfghjklmnpqrstvwxyz'.split('');
    var word = '', i, num;

    if (!limit) limit = 8;

    for (i = 0; i < (inclNumbers ? limit - 3 : limit); i++) {
        if (i % 2 == 0) { // even = vowels
            word += vowels[Math.floor(Math.random() * 4)]; 
        } else {
            word += constonants[Math.floor(Math.random() * 20)];
        } 
    }

    if (inclNumbers) {
        num = Math.floor(Math.random() * 99) + '';
        if (num.length == 1) num = '00' + num;
        else if (num.length == 2) num = '0' + num;
        word += num;
    }

    return word.substr(0, limit);
}

Permalink: http://codedumper.com/password-generator-js