Skip to content

Instantly share code, notes, and snippets.

@sagar290
Created June 17, 2023 08:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sagar290/71dfefcaa6fc2107da6ebeaba77cc432 to your computer and use it in GitHub Desktop.
Save sagar290/71dfefcaa6fc2107da6ebeaba77cc432 to your computer and use it in GitHub Desktop.
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