Find root of a number using Newton 's method | Function to return the square root of a number using Newtons method ; Assuming the sqrt of n as n only ; The closed guess will be stored in the root ; To count the number of iterations ; Calculate more closed x ; Check for closeness ; Update root ; Driver code
function squareRoot ( n , l ) { let x = n ; let root ; let count = 0 ; while ( true ) { count ++ ; root = 0.5 * ( x + ( n / x ) ) ; if ( Math . abs ( root - x ) < l ) break ; x = root ; } return root . toFixed ( 4 ) ; } let n = 327 ; let l = 0.00001 ; document . write ( squareRoot ( n , l ) ) ;