From 6abc8638488a35b6b6b562a8c9cbfd2e4023e75e Mon Sep 17 00:00:00 2001 From: colin Date: Wed, 27 Dec 2023 15:32:21 +0000 Subject: [PATCH] Update lib/key_generators/phonetic.js --- lib/key_generators/phonetic.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/key_generators/phonetic.js b/lib/key_generators/phonetic.js index 39d24a0..68da747 100644 --- a/lib/key_generators/phonetic.js +++ b/lib/key_generators/phonetic.js @@ -1,13 +1,25 @@ +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 isConsonant = Math.random() < 0.5; + let chooseConsonant = Math.random() < 0.5; // Randomly start with consonant or vowel for (let i = 0; i < keyLength; i++) { - text += isConsonant ? randConsonant() : randVowel(); - isConsonant = !isConsonant; // Toggle between consonant and vowel + text += chooseConsonant ? randConsonant() : randVowel(); + chooseConsonant = !chooseConsonant; // Alternate between consonant and vowel } return text; } + };