Skip to main content

Posts

Showing posts from January, 2018

JavaScript: Generating Random Text with Letters and Numbers

Without further ado, I typed this coding on something. Here goes, a function named generateSomething : /** * Generates random sequence of letters * (lowercase and uppercase) and numbers. * * @param {number} [a=6] - Length of the generated sequence. * Must be a positive integer. * @returns {string} A random string of the specified length. * */ function generateSomething(a) { let letters = "abcdefghijklmnopqrstuvwxyz"; let numbers = "0123456789"; let upperCase = letters.toUpperCase(); let length = 6; const result = []; if ( a?.constructor === Number && a > 0 && a % 1 === 0 ) length = a; const splitt = (a) => a.split(""); const randomise = (a) => { const l = a.length; const ix = Math.floor(Math.random() * l); return a[ix]; }; letters = splitt(letters); numbers = splitt(numbers); upperCase = splitt(upperCase); co...