What's the best way in JavaScript to test if a given parameter is a square number?

后端 未结 7 2608
忘了有多久
忘了有多久 2021-02-19 06:19

I created a function that will test to see if a given parameter is a square number.

Read about square numbers here: https://en.wikipedia.org/?title=Square_number

<
7条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-19 07:05

    Isn't this (Math.sqrt(number) % 1 === 0) basically enough? it just checks if the sqrt of the number is a whole number, if so, then it's a perfect square.

    Obviously, depending on what you want to do with that information, it may require extra code.

提交回复
热议问题