26 lines
632 B
JavaScript
26 lines
632 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 = true; // Always start with consonant
|
|
|
|
for (let i = 0; i < keyLength; i++) {
|
|
text += chooseConsonant ? randConsonant() : randVowel();
|
|
chooseConsonant = !chooseConsonant; // Alternate between consonant and vowel
|
|
}
|
|
|
|
return text;
|
|
}
|
|
|
|
};
|