26 lines
658 B
JavaScript
26 lines
658 B
JavaScript
const vowels = 'aeiou';
|
|
const consonants = 'bcdfghjklmnpqrstvwxyz';
|
|
|
|
const randOf = (collection) => {
|
|
return () => collection[Math.floor(Math.random() * collection.length)];
|
|
};
|
|
|
|
const randVowel = randOf(vowels);
|
|
const randConsonant = randOf(consonants);
|
|
|
|
module.exports = class PhoneticKeyGenerator {
|
|
|
|
createKey(keyLength) {
|
|
let text = '';
|
|
let chooseConsonant = Math.random() < 0.5; // Randomly start with consonant or vowel
|
|
|
|
for (let i = 0; i < keyLength; i++) {
|
|
text += chooseConsonant ? randConsonant() : randVowel();
|
|
chooseConsonant = !chooseConsonant; // Alternate between consonant and vowel
|
|
}
|
|
|
|
return text;
|
|
}
|
|
|
|
};
|