is-even
is-even copied to clipboard
A lesser space occupying alternative.
I have done it! No need to spend forever to tediously type in every possible number! Full word to number conversion included! (Examples are in the bottom of the index.js and README file.) I hope I get credit for this, I spent 5 hours of my day working on it. Have a nice day!
This is an overcomplicated solution: using nested loops and if
blocks is much more-confusing to new developers than a nice simple list of all the even numbers.
But its an NPM package. New developers will just need to install the package, and then enter the function with the parameter, for example: isEven(22) will return true. Or, isEven(‘twenty two’) will also work
True, but we also want the code itself to be understandable in order to attract more development and future PRs. The existing architecture is easy for anybody to extend either for their own use or to contribute back to the project.
Also true, but the existing architecture is finite and repetitive. I also see nothing confusing about my code. Can you tell me what you want me to change?
It's only finite because we haven't written out all the numbers yet. We'll get there in the end!
This is an overcomplicated solution: using nested loops and
if
blocks is much more-confusing to new developers than a nice simple list of all the even numbers.
How is this ‘overcomplicated’? If anything, the existing code is overcomplicated. All my code does is first check if it is a number or a number in word form(if it is, change it to numbers), and then divides by two. There is nothing complex about this. The existing code however, is just if statements and is a big file if all you want to do is to check if a number is even or not. And you said if blocks are confusing.
But if this npm package is a joke, then you can reject my PR. I’ll find a good use for it.
Oh, it was a joke :(
I think I’ll use this as my own npm package. This was fun!
Ideally you'd want to use bitwise over modulo for this kind of thing. In JS you get a bit of a perf boost and in other languages you get a decent perf boost (JS lacks INTs which is what bitwise needs, so JS needs to convert to INT then to FLOAT). Also, ideally you'd want to avoid unneeded branching as well as else
statements (so probs a good idea to remove the string check when unneeded). Branching generally slows things down and else
statements even further due to how CPUs optimize if
statements.
function IsEven(num) {
if (isNaN(+num)) {
return !!num.match(/(?:zero|two|four|six|eight|ten|twelve|fourteen|sixteen|eighteen|twenty|thirty|forty|fifty|sixty|seventy|eighty|ninety|hundred|thousand|million|billion|trillion|quadrillion)$/gi);
}
return !(+num & 1);
}