9

I have a piece of code which includes this:

var clusterCenters = [{"x": 1,"y": 1},{"x": 10,"y": 10}];
console.log(clusterCenters);

In Chrome 26:

enter image description here

In Firefox 21:

enter image description here

Why does this error occur?

I guess the error has to be in the following function.

function getKMeansInfo(k, mouseX, mouseY) {
    // choose cluster centers
    var clusterCenters = [{"x": 1,"y": 1},{"x": 10,"y": 10}];
    console.log(clusterCenters);

    for (iteration=0;iteration< 20;iteration++) {
        // for each object, check which cluster is nearest
        for (i=0; i<points.length; i++) {
            var distMin = 1000000;
            var clusterMin = 0;
            for(j=0; j<k; j++) {
                var dist = euklideanDist(clusterCenters[j], points[i]);
                if (dist < distMin) {
                    distMin = dist;
                    clusterMin = j
                }
            }
            points[i]["cluster"] = clusterMin;
        }

        // calculate center of cluster
        var clusterSum = new Array(k);
        for (i=0; i<k; i++) {
            clusterSum[i] = {"x":0, "y":0, "n":0};
        }
        for (i=0; i<points.length; i++) {
            clusterSum[points[i]["cluster"]]["x"] += points[i]["cluster"]["x"];
            clusterSum[points[i]["cluster"]]["y"] += points[i]["cluster"]["y"];
            clusterSum[points[i]["cluster"]]["n"] += 1;
        }
        for (i=0; i<k; i++) {
            if (clusterSum[i]["n"] > 0) {
                clusterCenters[i] = {
                    "x":clusterSum[i]["x"]/clusterSum[i]["n"],
                    "y":clusterSum[i]["y"]/clusterSum[i]["n"]};
            }
        }
    }

    return {"cluster":1, "radius":10};
}

The full source is on GitHub.

edit: Only the complete example is still online.

Any recommendations for use of JavaScript are also welcome (I'm new to JavaScript).

I've just recognized that using floating point numbers in the example above gives less errors ... but the error still occurs. As I never divide through "x" or "y", but only through "n" (and I check if n is 0 bevore dividing) I don't see how I can get NaN.

1
  • 3
    console.log implementations are sometimes "live". They don't necessarily represent the object when it was logged, but represent the object's "current state". You could check out all code that alters the clusterCenters, especially the last loop where you divide values. There must be a non-number value there.
    – Joseph
    May 21, 2013 at 7:22

1 Answer 1

4

As has been mentioned console logging can be tricky. Chrome, in particular displays single valued objects in a live format.

If you are logging multiple times to see the evolution of an object, use the following :

 console.log (JSON.stringify (obj));

This will ensure that you see the value at the time the console.log was executed. This does rely on JSON.stringify and will fail for complex, recursive objects so this won't help for DOM objects for example.

Debugging JavaScript

When you want to debug JavaScript, you can either use Firefox with Firebug or Chrome and its built-in developer tools. You open them with Ctrl+Shift+I. They look like this:

enter image description here

with developer tools, you can set breakpoints which makes debugging much easier.

1
  • The error in my code was in points[i]["cluster"]["x"]. It should have been points[i]["x"]. So I've tried to access a dictionary although it was a number which seems to cause NaN. I've found this error by using Chromes developer tools. May I add a hint to developer tools with a screenshot to your answer? May 22, 2013 at 4:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.