Calculating n | Javascript Program to find n - th real root of x ; Initialize boundary values ; used for taking approximations of the answer ; Do binary search ; Driver code
function findNthRoot ( x , n ) { let low , high ; if ( x >= 0 && x <= 1 ) { low = x ; high = 1 ; } else { low = 1 ; high = x ; } let epsilon = 0.00000001 ; let guess = parseInt ( ( low + high ) / 2 , 10 ) ; while ( Math . abs ( ( Math . pow ( guess , n ) ) - x ) >= epsilon ) { if ( Math . pow ( guess , n ) > x ) { high = guess ; } else { low = guess ; } guess = ( low + high ) / 2 ; } document . write ( guess ) ; } let x = 5 ; let n = 2 ; findNthRoot ( x , n ) ;