-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
generate words starting with letter #42
base: main
Are you sure you want to change the base?
Changes from all commits
bb6d2ea
e0f7d22
4f23904
9ecbfd1
3940857
26d1365
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1965,11 +1965,13 @@ const longestWordSize = wordList.reduce((longestWord, currentWord) => | |
).length; | ||
|
||
export function generate(options) { | ||
// initalize random number generator for words if options.seed is provided | ||
// Initialize random number generator for words if options.seed is provided | ||
const random = options?.seed ? new seedrandom(options.seed) : null; | ||
|
||
const { minLength, maxLength, ...rest } = options || {}; | ||
// Destructure options | ||
const { minLength, maxLength, prefix, ...rest } = options || {}; | ||
|
||
// Function to generate a random word | ||
function word() { | ||
let min = | ||
typeof minLength !== "number" | ||
|
@@ -1986,14 +1988,31 @@ export function generate(options) { | |
let rightSize = false; | ||
let wordUsed; | ||
while (!rightSize) { | ||
wordUsed = generateRandomWord(); | ||
// Generate a random word with the provided prefix (if any) | ||
wordUsed = generateRandomWord(prefix); | ||
rightSize = wordUsed.length <= max && wordUsed.length >= min; | ||
} | ||
return wordUsed; | ||
} | ||
|
||
function generateRandomWord() { | ||
return wordList[randInt(wordList.length)]; | ||
// Function to generate a random word with an optional prefix | ||
function generateRandomWord(prefix = null) { | ||
let result = null; | ||
|
||
// Filter the wordList to find words starting with the specified prefix | ||
if (prefix) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's make sure it's a string:
|
||
const filteredList = wordList.filter((word) => | ||
word.startsWith(prefix) | ||
); | ||
if (filteredList.length > 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Throw an exception ( |
||
const randomIndex = Math.floor(Math.random() * filteredList.length); | ||
result = filteredList[randomIndex]; | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
else { | ||
result = wordList[randInt(wordList.length)]; | ||
} | ||
return result; | ||
} | ||
|
||
// limits the size of words to the minimum and maximum possible | ||
|
@@ -2081,4 +2100,4 @@ export function count(options) { | |
return wordList.filter( | ||
(word) => word.length >= minLength && word.length <= maxLength | ||
).length; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While it was already possible in this loop, adding
prefix
increases the likelihood that we'll never find a word and get stuck in an infinite loop, which is problematic for sure. So please refactorgenerateRandomWord
to also takemin
andmax
and include them in yourfilter
criteria. That way we will know if it's impossible to satisfy the request due to any combination ofmin
,max
andprefix
and can throw one clean exception. And this "rightSize" while loop can go away.Probably generateRandomWord should just be merged with this function.