Archive

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;
}

Upgrade to WordPress 2.5

I’ve upgraded to WordPress 2.5. In the process I made some nice backups of my site, and cleaned up my internal site structure.

I’m now running on a nightly of the K2 theme, and hope to switch to a general release once one is compatible with 2.5.

I must say I’m quite impressed with WordPress.

JavaScript appendChild returning null

Update: Thanks to Michael in the comments for the solution. I forgot that JS begins executing before the DOM is finished loading.

I’m trying to get the following code to work, but it’s failing:

<html>
    <head>
        <title></title>
        <script type="text/javascript">

        var ul = document.getElementById("list");

        var li = document.createElement("li");
        var litext = document.createTextNode("test");
        li.appendChild(litext);
        ul.appendChild(li);

        </script>
    </head>
    <body>
        <h1></h1>
        <ul id="list">
        </ul>
    </body>
</html>

Any thoughts?