Created
June 17, 2023 08:15
-
-
Save sagar290/71dfefcaa6fc2107da6ebeaba77cc432 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sqrt(number) { | |
if (number < 0) { | |
throw new Error("Square root of a negative number is undefined"); | |
} | |
let guess = number / 2; | |
const precision = 0.0001; // Adjust the precision as needed | |
while (Math.abs(guess * guess - number) > precision) { | |
guess = (guess + number / guess) / 2; | |
} | |
return guess; | |
} | |
const number = 16; | |
const result = sqrt(number); | |
console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment