Hacker News new | past | comments | ask | show | jobs | submit login

Telling you how to do it was not actually my point too, sorry if it sounded patronizing, that was not the intention. I was writing this for all the readers here, starting from the beginning for completeness' sake, at the risk of stating the obvious.

n % 2 or n & 1 is something you likely get used to if you encounter it frequently enough, and at least something you should recognize after the first time you encounter it. And most people should have encountered it at least once because that's one of the things we learn to do when learning programming (but I guess Ten Thousand [1] can apply). If you don't encounter it enough to be used to it, then the rare gymnastic should not be a big issue neither. You need to be able to recognize it because it's in the wild.

If having an isOdd or isEven function around makes the code more readable for you anyway it's fine, especially if the function can be inlined, but pulling an external dependency for this is overkill.

[1] https://xkcd.com/1053/




> if you encounter it frequently enough

That's a huge if!

So abstracting a one line in a function is actually good. Putting it up on npm is debatable.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: