squareRoot

by Krishna Ananthi

HTML

//square root of element, round off

JavaScript

function squareRoot(x) {
  let s = 0,
    e = x / 2,
    mid, ans = -1;
  while (s < e) {
    mid = Math.floor((s + e) / 2);
    console.log(mid)
    if (mid * mid === x) {
      return mid;
    } else if (mid * mid > x) {
      e = mid - 1;
    } else {
      ans = mid
      s = mid+1;
    }
  }
  return ans;
}
console.log(squareRoot(27))