-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Fix incorrect condition in "Chains of computations" example #8109
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
Conversation
Fixes reactjs#8097 The refactored example in the "Chains of computations" section uses an incorrect condition that changes the game logic from the original. The original Effect-based code advances the round after 4 gold cards: - Increments first (0→1, 1→2, 2→3, 3→4) - Then checks `goldCardCount > 3` (true when count is 4) The refactored code with `goldCardCount <= 3` allows 5 gold cards: - Checks before incrementing - Allows counts 0, 1, 2, 3 to increment (4 values) - Advances on the 5th card (when count is 4) This fix changes the condition to `goldCardCount < 3`: - Allows counts 0, 1, 2 to increment (3 values) - Advances on the 4th card (when count is 3) - Matches the original behavior Verified by tracing execution logic and building the docs site locally.
|
Hi @PaulyBearCoding! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
Size changes📦 Next.js Bundle Analysis for react-devThis analysis was generated by the Next.js Bundle Analysis action. 🤖 This PR introduced no changes to the JavaScript bundle! 🙌 |
|
I don't see the 5th card: if (nextCard.gold) {
if (goldCardCount <= 3) {
// doesn't enter if there are 4 cards
setGoldCardCount(goldCardCount + 1);
} else {
// ends at only 4 cards
setGoldCardCount(0);
setRound(round + 1);
if (round === 5) {
alert('Good game!');
}
}
} |
@rickhanlonii, the fact that the program doesn't enter the first block when
|
|
I see, so it takes 5 clicks with a gold card next. I was wrong, I confirmed your right in this sandbox: |
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.
Nice catch, thanks for helping me understand it
Fixes #8097
Problem
In the "Chains of computations" section, the refactored example uses an incorrect condition that changes the game logic from the original:
This allows 5 gold cards before advancing the round, which differs from the original Effect-based example that advances after 4 gold cards.
Solution
Changed the condition from
<=to<:Why This Fix is Correct
The original "bad" example increments first, then checks:
This advances after 4 cards (when count reaches 4).
The fixed example checks before incrementing:
<= 3: Allows counts 0,1,2,3 to increment → 4 values → advances on 5th card ❌< 3: Allows counts 0,1,2 to increment → 3 values → advances on 4th card ✅Testing
Verified the fix by:
Changes
src/content/learn/you-might-not-need-an-effect.mdgoldCardCount <= 3→goldCardCount < 3