JSFiddle - React, Tailwind, and code Playground

by Afzaal Ahmad Zeeshan

HTML

<input type="text" onchange="squareRoot(this.value)">

JavaScript

function squareRoot(value) {
    if(value == 0 || value == 1) {
		return value;
	}
	
	var start = 1, end = value;
	var mid = value / 2;
	while (start <= end) {
	    var current = mid * mid;
	    if(current == value) {
			return mid;
		}
		
		if(current > value) {
		    end = mid - 1;
		} else {
		    start = mid + 1;
		}
	}
	
	alert(`Is the answer ${mid}?`);
}