r/shittyprogramming • u/evilgwyn • Jun 07 '21
Super efficient isEven function in javascript (uses unrolled loops for speed)
function isEven(n) {
switch (n) {
case 0:
return true;
case 1:
return false;
case 2:
return true;
case 3:
return false;
case 4:
return true;
case 5:
return false;
case 6:
return true;
case 7:
return false;
case 8:
return true;
case 9:
return false;
case 10:
return true;
case 37:
return false;
}
}
Let me know if you need me to add any more numbers.
41
Upvotes
20
u/[deleted] Jun 07 '21
At the bottom of switch use:
if (n > 10) { return isEven(n - 10); }7
u/[deleted] Jun 07 '21
This is slow. You should put each value in a hashmap for constant runtime. smh my head.
11
u/schwester Jun 07 '21
function isEven(n) {return !(n & 1)}At first I fix it but then I realized it is a joke :P
4
u/[deleted] Jun 07 '21
3
u/Josemite Jun 07 '21
Can you add 37? Thanks!
2
u/RapidCatLauncher Jun 07 '21
If you need more numbers, make it call another function that programmatically creates the code and inserts it into the source.