A JavaScript sqrt() function.

I know there’s a Math.sqrt() function; I just wanted to create my own. Is this how most people would implement a sqrt() function in JavaScript if one didn’t already exist?

sqrt = function(x) {
    j = x / 2;
    min = 0;
    max = x;
    for (i=0; i<100; i++) {
        if (j * j > x) {
            max = j;
        } else {
            min = j;
        }
        // print(max - min);
        j = max - ((max - min)/2);
    }
    return j;
}

0 Responses to “A JavaScript sqrt() function.”


  1. No Comments

Leave a Reply