The correct answer is from @IsaacRay; you simply had the polarity of your test reversed.
The accepted answer, for what it's worth, is about ten times slower than using %
.
You could also use Math.floor
which is about as fast as %
:
if (Math.floor(dTotal) === dTotal && Math.floor(nTotal) === nTotal) {
Math.floor
basically removes the decimal part of the number. By comparing it to the original number, we can see if the number is an integer.
You might be tempted to use Number.isInteger
if available, but it is very slow; twice as slow as even parseInt
, twenty times as slow as %
or Math.floor
.
JSPerf: http://jsperf.com/testing-if-a-number-is-an-integer