https://www.npmjs.com/package/is-even
don’t look at the weekly downloads if you are faint of heart.
To be fair in a dynamic typed language with dumb string to int coercions, I kinda get why such a library would exists. So it’s more a symptom of terrible language design than modern dependency hell.
in a dynamic typed language with dumb string to int coercions, I kinda get why such a would library exists.
If string return nan, else % 2
So it’s more a symptom of terrible language design than modern dependency hell.
Dependency chain: is-even depends on is-odd which depends on is-number
If string return nan, else % 2
So now you return a number type if it’s a string and a boolean if it’s an integer. How does that make sense?
The is-even lib exists to sanitize input by throwing an exception which imho is better.
Edit: having looked at the code better. Apparently it still allows string coercion (boo). It only checks for non integer numbers.
Good point, but you can do if === true… and else if === false…
But definitely better to throw an error instead of nan.
deleted by creator
If you really want to see some horror, follow the dependencies
https://10xengineersqualityprogramming.github.io/ https://www.npmjs.com/package/@falsejs/falsejs This is hilarious, has 262 of the best useless dependencies. In all seriousness though how does anyone ever audit a npm package, it’s dependency hell!
Boss: don’t spend any time on it, just vibe code a solution.
You: sure, I enjoy receiving a salary, what could go wrong?
Mood…
Fixed
boolean isOdd(int num) { if(num == 1) return true; if(num > 0) return isEven(num - 1); else return isEven(num + 1); } boolean isEven(int num) { if(num > 0) return isOdd(num - 1); else return isOdd(num + 1); }
the downside with this approach is that it will eventually terminate. the version in the original post has the advantage of giving me plenty of time to contemplate life’s many mysteries.
What can I say, I’m a performance nerd.
isEeven(∞);
Why the complicated if statements to check the sign? Just let the number overflow. Would be functionaly the same, and result in much prettier code.
That’s a platform dependent change. Overflow is undefined behavior. I’d rather have my code portable so it can run on my Univac 1101.
isEven(0) -> true; isEven(Num) -> isOdd(Num-1). isOdd(0) -> false; isOdd(Num) -> isEven(Num-1).
Hmm.
isEven(-2)…<out of stack error>
When it fails, it at least points you to the site where everyone asks for help.
Closed as unclear
easy fix… if infinity return false.
mathematical breakthrough bonus proof: all numbers are neither even nor odd.
Removed by mod
Removed by mod
bool isOdd(int num) { const oddNumbers = []; for (let i = 1; i <= 10000000; i += 2) { oddNumbers.push(i); } if (oddNumbers.includes(num) { return true; } }
isOdd(10000001);
this incident has been reported
You should make it
oddNumbers.includes(num%10000000)
…And if not, unicorns!
isOdd(-1);
deleted by creator
Maybe memo just to be safe, but LGTM!
Oh, Python!