Discuss possible ways to write a function
isInteger(x)
that determines if x is an integer.
There are a lot of ways that this can be solved. Without looking at the answer, two stood out to me as potential options. The first thought and what I think is the cleanest option takes me back to my middle school math days. An integrer to the power of 0 equals itself so a function raising x^0
would do the trick.
function isInteger(x) { return (x^0) === x; }
The other solution I came up with is to use some javascripting. Javascript had three really helpful properties that could work for this Math.floor()
, Math.ciel()
, and Math.round()
. These all round the number either up or down. If it were an integer, it would not change.
function isInteger(x) { return Math.round(x) === x; }
These are just two of many options. Have another solution? Post it in the comments or tweet it to me @MaureenCodes.