/** * @param {number} x * @return {number} */ var mySqrt = function (x) { let start = 0; let end = x; let mid = Math.floor((start + end) / 2); if (x === 1) return 1; while (start < mid) { if (mid ** 2 === x) { break; } if (mid ** 2 > x) { end = mid; mid = Math.floor((start + end) / 2); } if (mid ** 2 < x) { start = mid; mid = Math.floor((start + end) / 2); } } return mid }; console.log(mySqrt(8));