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

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

    I think that this one is a shorter and a cleaner option:

      var isSquare = function(n) {
    
      return Number.isInteger(Math.sqrt(n));
    };
    
    isSquare(25); //true
    
    

    for even shorter and cleaner than that you could use:

    var isSquare = n => Number.isInteger(Math.sqrt(n));
    
    isSquare(25);//true
    

提交回复
热议问题