Last active
April 29, 2020 04:34
-
-
Save AlvisonHunterArnuero/b7ee3ac6d00784c91851b09467eeae54 to your computer and use it in GitHub Desktop.
This function will generate a random string based on the parameter number, useful for temp passwords
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This function will generate a random string based on the parameter number, useful for temp passwords | |
const fnRandomString = (argLength) => { | |
let generatedString = ''; // receptor of the generated string, this will contain the x amount of character based on args | |
let baseString = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@*+&^%$#!'; | |
let charactersLength = baseString.length; | |
// refactor this to a ES6 friendly version, by using one of the looping helpers that we count with in ES6 | |
[...baseString].every(function(element,index) { | |
// start building the string getting a random character each time based on the baseString | |
generatedString += baseString.charAt(Math.floor(Math.random() * charactersLength)) ; | |
// using the index of this helper, if it is equal to args-1 we stop the loop, otherwise | |
// we keep adding characters to the generated String, this is not a good practice btw... | |
return index !== argLength-1; // trick to break this loop, not an official version :) | |
} ); | |
return generatedString; // Once is ready, the funtcion returns the results :) | |
} | |
console.log("Final Result: ", fnRandomString(6)); // duh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment