After doing a sqrt()
How can I be check to see if the result contains only whole numbers or not?
I was thinking Regex to check for a decimal – if it contains a decimal, that means it didn’t root evenly into whole numbers. Which would be enough info for me.
but this code isnt working…
JavaScript
x
4
1
result = sqrt(stringContainingANumber);
2
decimal = new RegExp(".");
3
document.write(decimal.test(result));
4
I bet there’s other ways to accomplish the same thing though.
Advertisement
Answer
. means any char. You have to quote the dot. “.”
Or you could test
JavaScript
1
4
1
if (result > Math.floor(result)) {
2
// not an decimal
3
}
4