modularized random number generation logic

This commit is contained in:
Alex Booker 2015-08-14 17:54:07 +01:00
parent b38eadd4ed
commit e13220c2fb

View File

@ -1,6 +1,9 @@
var passwordGenerator = (function() { var passwordGenerator = (function() {
return { var generateRandomNum = function (max) {
generatePassword: function (options) { return Math.floor(Math.random() * max);
};
var generatePassword = function (options) {
var uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXTZ"; var uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXTZ";
var lowercase = "abcdefghiklmnopqrstuvwxyz"; var lowercase = "abcdefghiklmnopqrstuvwxyz";
var numbers = "0123456789"; var numbers = "0123456789";
@ -20,10 +23,13 @@ var passwordGenerator = (function() {
} }
var result = ""; var result = "";
for (var i = 0; i < options.passwordLength; i++) { for (var i = 0; i < options.passwordLength; i++) {
var randomNum = Math.floor(Math.random() * candidates.length); var randomNum = generateRandomNum(candidates.length);
result += candidates.substring(randomNum, randomNum + 1); result += candidates.substring(randomNum, randomNum + 1);
} }
return result; return result;
} };
return {
generatePassword: generatePassword
}; };
})(); })();