views:

345

answers:

3

Racking my brains on this one. I have the code below: the first stages of a JavaScript game. All the objects are well-defined and I'm using jQuery for DOM interaction. The puzzle is created with the following JS code:

var mypuzzle = new puzzle("{solution:'5+6+89',equations:[['5+3=8',23,23],['5+1=6',150,23],['5+3=6',230,23]]}");

However, the loop at the bottom of the code won't go further than the first iteration. Any idea why? No errors are thrown at all.

function equationBox(equation, top, left) {//draggable equation box
    this.reposition = function() {
     this.top = 0;
     this.left = 0;
    }
    this.top = 0;//make random
    this.left = 0;//make random
    this.equation = equation;
    if(top && left) {
     this.top = top; 
     this.left = left;
    }
    this.content = this.equation.LHS.string + '<span> = </span>' + this.equation.RHS.string;
    this.DOM = $('<li>').html(this.content);
}


function puzzle(json) {

    this.addEquationBox = function(equationBox) {
     $('#puzzle #equations').append(equationBox.DOM);
    }

    this.init = function() {
     //this.drawPuzzleBox();
     this.json = JSON.parse(json);
     this.solution = new expression(this.json.solution || '');
     this.equations = this.json.equations || [];
     var iterations = this.equations.length;
     for(i=0;i<iterations;i++)
     {
      console.log(i);
      this.addEquationBox(new equationBox(stringToEquation(this.equations[i][0]),this.equations[i][1], this.equations[i][2])); 
     }
    }
    this.init();
}
+10  A: 

Possibly your failure to scope your counter variable is doing it, especially if you make a habit of it (since you're using the global variable of that name, and any loops you wrote in any code you're calling may be doing the same thing). Try:

for(var i=0;i<iterations;i++)
chaos
+1. THE classic Javscript gotcha.
AnthonyWJones
Excellent - thanks
wheresrhys
+1  A: 

because this.equations = this.json.equations || [] , and, since this.json.equations is undefined, it get assigned to []

Rodrigo
That would result in the loop running for zero iterations, not one.
chaos
Why do you believe json.equations is undefined? If JSON has parsed the input json a para,eter it ought to be an array
AnthonyWJones
it's a chance. As we don't have the whole project available to take a peek, we can just make assumptions and use the experience to suggest possibly errors. I've just used my crystal ball. Indeed, the error could be more plausible to be cause by a scope mistake using "i" as a global variable in the for statement.
Rodrigo
A: 

Assuming you're using JSON.parse as defined at http://www.json.org/json2.js, it appears that your json string is not parsing properly:

var string1 = "{solution:'5+6+89',equations:[['5+3=8',23,23],['5+1=6',150,23],['5+3=6',230,23]]}"
JSON.parse(string1); // throws SyntaxError("JSON.parse")

When I use JSON.stringify, defined in the same file, to create a JSON string from your object:

var obj = {solution:'5+6+89',equations:[['5+3=8',23,23],['5+1=6',150,23],['5+3=6',230,23]]}
var string2 = JSON.stringify(obj);
// {"solution":"5+6+89","equations":[["5+3=8",23,23],["5+1=6",150,23],["5+3=6",230,23]]}
JSON.parse(string2); // returns a proper object

Note that the string that JSON.stringify is creating is different than the one you are trying to use, which might be the cause of your problem.

Daniel Vandersluis