Skip to content Skip to sidebar Skip to footer

Is Javascript Namespace Polluted?

I do not have a good grasp of the js namespace and am WAGing* re the title, but that's one of my guesses about what's happening. WAG = Wild Guess My app is crashing (dramatically

Solution 1:

My guess would be

var window = {};

window is special, so creating a global variable named window is begging for trouble.


Solution 2:

Your while loop runs infinitely on the third pass because it doesn't meet the condition.


Solution 3:

At some point, arrDone will contain the numbers 1, 2, and 3, as produced by your random generator (which will never produce 5, btw). In that case, nextQues() does not abort and return five (as arrDone.lenght == 3), and will enter the loop. Your random generator produces nothing but the numbers 1, 2, and 3, which always are already in the array, so the if-condition (that would end the loop) is never fulfilled. You have an infinite loop generating random numbers.

I guess you want

function nextQues() {
    var l = 4;
    if (window.arrDone.length >= l)
        return l+1;

    while (true) {
        var nn = Math.floor(Math.random() * l) + 1; // generate 1, 2, 3 or 4
        if (window.arrDone.indexOf(nn) == -1) {
            window.arrDone.push(nn);
            return nn;
        }
    }
}

Post a Comment for "Is Javascript Namespace Polluted?"