Javascript Algorithm Number Square Number calculate 2

Description

Javascript Algorithm Number Square Number calculate 2

function sqrtInt(number) {
    if (number == 0 || number == 1) return number;

    var start = 1,
        end = number,// w ww  . j a va2 s  .  co m
        ans;

    while (start <= end) {
        let mid = parseInt((start + end) / 2);

        if (mid * mid == number)
            return mid;

        if (mid * mid < number) {
            start = mid + 1; // use the upper section
            ans = mid;
        } else {
            end = mid - 1; // use the lower section
        }
    }
    return ans;
}
let a = sqrtInt(9);
console.log(a);



PreviousNext

Related