
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Finding square root of a number without using Math.sqrt() in JavaScript
We are required to write a JavaScript function that takes in a positive integer as the only argument. The function should find and return the square root of the number provided as the input.
Example
Following is the code −
const squareRoot = (num, precision = 0) => { if (num <= 0) { return 0; }; let res = 1; const deviation = 1 / (10 ** precision); while (Math.abs(num - (res ** 2)) > deviation) { res -= ((res ** 2) - num) / (2 * res); }; return Math.round(res * (10 ** precision)) / (10 ** precision); }; console.log(squareRoot(16)); console.log(squareRoot(161, 3)); console.log(squareRoot(1611, 4));
Output
Following is the output on console −
4 12.689 40.1373
- Related Articles
- Finding square root of a number without using library functions - JavaScript
- Finding square root of a non-negative number without using Math.sqrt() JavaScript
- How to perform square root without using math module in Python?
- Check if a number is perfect square without finding square root in C++
- Check for perfect square without using Math libraries - JavaScript
- Square root function without using Math.sqrt() in JavaScript
- Get minimum number without a Math function JavaScript
- Get square root of a number using Math.sqrt in Java
- How to get the square root of a number in JavaScript?
- 8086 program to find the square root of a perfect square root number
- Returning a range or a number that specifies the square root of a number in JavaScript
- Find the number of digit in the square root of the given number ( without any calculation): $529$.
- Find the number of digit in the square root of the given number ( without any calculation): $1444$.
- Find the number of digit in the square root of the given number ( without any calculation): $17161$.
- Find the number of digit in the square root of the given number ( without any calculation): $4601025$.

Advertisements