refactor phonetic.js to match test requirements.

This commit is contained in:
colin 2023-12-27 14:32:21 +00:00
parent 4ae1756c35
commit 23c47e49ec
1 changed files with 3 additions and 17 deletions

View File

@ -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;
}
};