From e13220c2fb63daedf5746ad3bf95d044a87a48aa Mon Sep 17 00:00:00 2001 From: Alex Booker Date: Fri, 14 Aug 2015 17:54:07 +0100 Subject: [PATCH] modularized random number generation logic --- js/passwordGenerator.js | 56 +++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/js/passwordGenerator.js b/js/passwordGenerator.js index da85dcf0..e25e611f 100644 --- a/js/passwordGenerator.js +++ b/js/passwordGenerator.js @@ -1,29 +1,35 @@ var passwordGenerator = (function() { - return { - generatePassword: function (options) { - var uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXTZ"; - var lowercase = "abcdefghiklmnopqrstuvwxyz"; - var numbers = "0123456789"; - var punct = ".,-/#!$%^&*;:{}=-_`~()]"; - var candidates = ''; - if (options.includeUppercaseChars) { - candidates += uppercase; - } - if (options.includeLowercaseChars) { - candidates += lowercase; - } - if (options.includeNumbers) { - candidates += numbers; - } - if (options.includePunctuationChars) { - candidates += punct; - } - var result = ""; - for (var i = 0; i < options.passwordLength; i++) { - var randomNum = Math.floor(Math.random() * candidates.length); - result += candidates.substring(randomNum, randomNum + 1); - } - return result; + var generateRandomNum = function (max) { + return Math.floor(Math.random() * max); + }; + + var generatePassword = function (options) { + var uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXTZ"; + var lowercase = "abcdefghiklmnopqrstuvwxyz"; + var numbers = "0123456789"; + var punct = ".,-/#!$%^&*;:{}=-_`~()]"; + var candidates = ''; + if (options.includeUppercaseChars) { + candidates += uppercase; } + if (options.includeLowercaseChars) { + candidates += lowercase; + } + if (options.includeNumbers) { + candidates += numbers; + } + if (options.includePunctuationChars) { + candidates += punct; + } + var result = ""; + for (var i = 0; i < options.passwordLength; i++) { + var randomNum = generateRandomNum(candidates.length); + result += candidates.substring(randomNum, randomNum + 1); + } + return result; + }; + + return { + generatePassword: generatePassword }; })();