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;
}
This entry was posted in General. Bookmark the permalink.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>