Skip to content
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

Fun remainder hack for capping numbers #16

Open
oliverjam opened this issue Feb 11, 2022 · 1 comment
Open

Fun remainder hack for capping numbers #16

oliverjam opened this issue Feb 11, 2022 · 1 comment

Comments

@oliverjam
Copy link

setEnemyX((prevX) => {
if (prevX === 80) {
prevX = 0;
}
return prevX + 2;
});

Here's a fun trick for "looping" a value back around

setEnemyX((prevX) => (prevX + 2) % 80)

Using the remainder effectively resets any value past 80 back to 0. E.g. (80 + 2) % 80 is 2. (100 + 2) % 80 is 22

@oliverjam
Copy link
Author

Oh but I'm not criticising your solution at all: it's clear and easy to understand and it works. I just think the remainder thing is neat 😅

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

No branches or pull requests

1 participant