From 23c47e49ecb6f70a309b0ca4b1d620a5e3698987 Mon Sep 17 00:00:00 2001 From: colin Date: Wed, 27 Dec 2023 14:32:21 +0000 Subject: [PATCH] refactor phonetic.js to match test requirements. --- lib/key_generators/phonetic.js | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/lib/key_generators/phonetic.js b/lib/key_generators/phonetic.js index f281f6b..39d24a0 100644 --- a/lib/key_generators/phonetic.js +++ b/lib/key_generators/phonetic.js @@ -1,27 +1,13 @@ -// Draws inspiration from pwgen and http://tools.arantius.com/password - -const randOf = (collection) => { - return () => { - return collection[Math.floor(Math.random() * collection.length)]; - }; -}; - -// Helper methods to get an random vowel or consonant -const randVowel = randOf('aeiou'); -const randConsonant = randOf('bcdfghjklmnpqrstvwxyz'); - module.exports = class PhoneticKeyGenerator { - - // Generate a phonetic key of alternating consonant & vowel createKey(keyLength) { let text = ''; - const start = Math.round(Math.random()); + let isConsonant = Math.random() < 0.5; for (let i = 0; i < keyLength; i++) { - text += (i % 2 == start) ? randConsonant() : randVowel(); + text += isConsonant ? randConsonant() : randVowel(); + isConsonant = !isConsonant; // Toggle between consonant and vowel } return text; } - };