Created
December 29, 2016 16:33
-
-
Save noctarius/f49c1980eb9823da2cbd953ba5930b7f to your computer and use it in GitHub Desktop.
I had the feeling I finally need to solve this issue! :)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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