Check for perfect square in JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

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
raja
Published on 15-Oct-2020 08:41:02
Advertisements