- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Returning a range or a number that specifies the square root of a number in JavaScript
Problem
We are required to write a JavaScript function that takes in an integer n and returns either −
- an integer k if n is a square number, such that k * k == n or
- a range (k, k+1), such that k * k < n and n < (k+1) * (k+1).
Example
Following is the code −
const num = 83; const squareRootRange = (num = 1) => { const exact = Math.sqrt(num); if(exact === Math.floor(exact)){ return exact; }else{ return [Math.floor(exact), Math.ceil(exact)]; }; }; console.log(squareRootRange(num));
Output
Following is the console output −
[9, 10]
- Related Articles
- How to get the square root of a number in JavaScript?
- Finding square root of a number without using Math.sqrt() in JavaScript
- 8086 program to find the square root of a perfect square root number
- Returning number of digits in factorial of a number in JavaScript
- Returning the expanded form of a number in JavaScript
- Finding square root of a number without using library functions - JavaScript
- Finding square root of a non-negative number without using Math.sqrt() JavaScript
- Are the square roots of all positive integers irrational? If not, give an example of the square root of a number that is a rational number.
- Reversing the bits of a decimal number and returning new decimal number in JavaScript
- 8085 program to find square root of a number
- 8086 program to find Square Root of a number
- How to calculate square root of a number in Python?
- Get square root of a number using Math.sqrt in Java
- Outputting a random number that falls in a certain range in JavaScript
- What is a number which is a square and square root itself?

Advertisements