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

后端 未结 7 2636
忘了有多久
忘了有多久 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:22

    I would definitely go for:

    var isSquare = function (n) {
        return Math.sqrt(n) % 1 === 0;
    };
    

    PS: 0 is a square number for those who wonder

    Demo

    0 讨论(0)
提交回复
热议问题