Skip to content

Conversation

@smysh
Copy link

@smysh smysh commented Jan 19, 2023

No description provided.


const whichLetter = (index, lp_copy) => {
for (let letter of Object.keys(lp_copy)) {
if (index === 0) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to understand this code better. I've taken about 10 minutes to try to puzzle out the purpose of index and why it decrements. I'm guessing that you sat with this code for some time! Maybe this could be a good example of how judicious uses of comments can help clarify code :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should have added more comments for sure.

index is the random value that is generated between 0 and 98 (98 is the sum of all the values in the letter_pool- it is calculated in a separate helper function.). So a letter with more values in the letter_pool has a higher probability of draw. This helps so we can get more vowels to make words with.

The whichLetter function, finds the letter associated with the index in the letter_pool. It keeps on decrementing the index by the value of the letters found in the letter_pool. We keep searching till index == 0, that's when the current letter is the one that's at the randomly picked index and we return it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants