最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why is my for loop stopping after one iteration? - Stack Overflow

programmeradmin0浏览0评论

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

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();
}
Share Improve this question edited Aug 27, 2009 at 20:16 chaos 124k34 gold badges306 silver badges310 bronze badges asked Aug 27, 2009 at 13:12 wheresrhyswheresrhys 23.6k21 gold badges96 silver badges165 bronze badges 2
  • Where is JSON.parse defined? – Daniel Vandersluis Commented Aug 27, 2009 at 13:32
  • What happens when you debug this? – Charlie Commented Aug 27, 2009 at 13:32
Add a ment  | 

3 Answers 3

Reset to default 11

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++)

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

Assuming you're using JSON.parse as defined at https://github./douglascrockford/JSON-js/blob/master/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.

发布评论

评论列表(0)

  1. 暂无评论