Skip to content

Instantly share code, notes, and snippets.

@noctarius
Created December 29, 2016 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noctarius/f49c1980eb9823da2cbd953ba5930b7f to your computer and use it in GitHub Desktop.
Save noctarius/f49c1980eb9823da2cbd953ba5930b7f to your computer and use it in GitHub Desktop.
I had the feeling I finally need to solve this issue! :)
// Found this awesome way in Java code once and had
// the strong feeling I need to port it to Javascript
// for the sake of all NodeJS developers!
var EvenOrOdd = function() {
function EvenOrOdd() {}
EvenOrOdd.prototype.isEven = function(value) {
var even = "02468";
var v = String(value);
var w = v.charAt(v.length - 1);
return even.indexOf(w) != -1;
}
EvenOrOdd.prototype.isOdd = function(value) {
return !isEven(value);
}
return new EvenOrOdd;
}
module.exports.EvenOrOdd = new EvenOrOdd();
(function() {
var test = new EvenOrOdd();
console.log(test.isEven(124));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment