Abstraction of Binary Search | Function to find X such that it is less than the target value and function is f ( x ) = x ^ 2 ; Initialise start and end ; Loop till start <= end ; Find the mid ; Check for the left half ; Store the result ; Reinitialize the start point ; Check for the right half ; Print the maximum value of x such that x ^ 2 is less than the targetValue ; Given targetValue ; ; Function Call
function findX ( targetValue ) { var start = 0 , end = targetValue ; var mid , result ; while ( start <= end ) { mid = start + parseInt ( ( end - start ) / 2 ) ; if ( mid * mid <= targetValue ) { result = mid ; start = mid + 1 ; } else { end = mid - 1 ; } } document . write ( result ) ; } var targetValue = 81 ; findX ( targetValue ) ;