
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check for Perfect Square Without Using Math Libraries 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 −
4, 16, 81, 441, 256, 729, 9801
Let’s write the code for this function −
const num = 81; const isPerfectSquare = num => { let ind = 1; while(ind * ind <= num){ if(ind * ind !== num){ ind++; continue; }; return true; }; return false; }; console.log(isPerfectSquare(81)); console.log(isPerfectSquare(9801)); console.log(isPerfectSquare(99)); console.log(isPerfectSquare(441)); console.log(isPerfectSquare(7648));
Output
Following is the output in the console −
true true false true false
Advertisements