Check for perfect square in JavaScript


We are required to write a JavaScript function that takes in a number and returns a boolean based on the fact whether or not the number is a perfect square.

Examples of perfect square numbers −

Some perfect square numbers are −

144, 196, 121, 81, 484

Example

The code for this will be −

const num = 484;
const isPerfectSquare = num => {
   let ind = 1;
   while(ind * ind <= num){
      if(ind * ind !== num){
         ind++;
         continue;
      };
      return true;
   };
   return false;
};
console.log(isPerfectSquare(num));

Output

The output in the console −

true

Updated on: 15-Oct-2020

777 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements