Skip to content
This repository was archived by the owner on Feb 26, 2026. It is now read-only.

generate words starting with letter#42

Open
SuryanshuTomar wants to merge 6 commits into
apostrophecms:mainfrom
SuryanshuTomar:suryanshutomar/-#17-generate-words-starting-with-letter
Open

generate words starting with letter#42
SuryanshuTomar wants to merge 6 commits into
apostrophecms:mainfrom
SuryanshuTomar:suryanshutomar/-#17-generate-words-starting-with-letter

Conversation

@SuryanshuTomar

@SuryanshuTomar SuryanshuTomar commented Jul 14, 2023

Copy link
Copy Markdown

Summary

Summarize the changes briefly, including which issue/ticket this resolves. If it closes an existing Github issue, include "Closes #[issue number]"

What are the specific steps to test this change?

For example:

  1. Run the website and log in as an admin
  2. Open a piece manager modal and select several pieces
  3. Click the "Archive" button on the top left of the manager and confirm that it should proceed
  4. Check that all pieces have been archived properly

What kind of change does this PR introduce?

(Check at least one)

  • Bug fix
  • New feature
  • Refactor
  • Documentation
  • Build-related changes
  • Other

Make sure the PR fulfills these requirements:

  • It includes a) the existing issue ID being resolved, b) a convincing reason for adding this feature, or c) a clear description of the bug it resolves
  • The changelog is updated
  • Related documentation has been updated
  • Related tests have been updated

If adding a new feature without an already open issue, it's best to open a feature request issue first and wait for approval before working on it.

Other information:

Work Done -
Created the function generateRandomWordStartingWith which will take a letter or a word and will return a random word from the list of word already present in the file. In case, an invalid input is given then, the function will return null.

Next PR - NA

Video -

indexjs-random-words-visual-studio-code-2023-07-14-20-28-07_nJ1iawRO.mp4

@SuryanshuTomar SuryanshuTomar changed the title Suryanshutomar/ #17 generate words starting with letter generate words starting with letter Jul 14, 2023

@boutell boutell left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you for the contribution!

This feature should be added as an enhancement to the existing generate function's options, so that it may be combined with other options. Also there no need to document it as a restriction to just one letter, as indeed your code already works with a longer prefix if a longer prefix is passed.

So I recommend naming the new option prefix and integrating it into the existing logic, rather than making a more limited standalone function that can't be combined with other functionality.

@SuryanshuTomar SuryanshuTomar requested a review from boutell August 20, 2023 07:20
@SuryanshuTomar

Copy link
Copy Markdown
Author

@boutell Here is the updated video -

recording-2023-08-20-124311_SGeuChJa.mp4

@BoDonkey

Copy link
Copy Markdown
Contributor

Howdy @SuryanshuTomar -
Your code is looking pretty good. There are a couple of more things you need to do, however. If you look at the PR template, we require that:
a) The changelog be updated. Don't assign a new version number. Instead, put it under a heading of "UNRELEASED"
b) Document how to use your new feature in the README.md file
c) Write tests (in /tests/test.js) to show that your feature works. You should have tests that pass for a variety of conditions, - returns a single word with a specific letter, returns 5 words all starting with the same letter, returns words of a minimum or maximum length starting with a letter. Plus at least one test where it fails - for example, what if the user wants a word of 8 letters that starts with 'z''?

@boutell boutell left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! We're getting close here but there are a few things to take care of. See my comments.

In addition:

  • Please add to the documentation.
  • Please add appropriate unit tests in tests/test.js.

Comment thread index.js
let result = null;

// Filter the wordList to find words starting with the specified prefix
if (prefix) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's make sure it's a string:

if (typeof prefix === 'string')

Comment thread index.js
const randomIndex = Math.floor(Math.random() * filteredList.length);
result = filteredList[randomIndex];
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

} else { (follow our style for if/else whitespace)

Comment thread CHANGELOG.md Outdated
## UNRELEASED

Added Function generateRandomWordStartingWith()
- That will take a string an input.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Needs a new changelog entry mentioning the new option.

Comment thread index.js
const filteredList = wordList.filter((word) =>
word.startsWith(prefix)
);
if (filteredList.length > 0) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Throw an exception (throw new Error('Unavailable')) if no words are available meeting the criteria.

Comment thread index.js
while (!rightSize) {
wordUsed = generateRandomWord();
// Generate a random word with the provided prefix (if any)
wordUsed = generateRandomWord(prefix);

Copy link
Copy Markdown
Member

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 refactor generateRandomWord to also take min and max and include them in your filter criteria. That way we will know if it's impossible to satisfy the request due to any combination of min, max and prefix and can throw one clean exception. And this "rightSize" while loop can go away.

Probably generateRandomWord should just be merged with this function.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants