admin管理员组文章数量:1344533
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
3 Answers
Reset to default 11Possibly 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.
本文标签: javascriptWhy is my for loop stopping after one iterationStack Overflow
版权声明:本文标题:javascript - Why is my for loop stopping after one iteration? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743787303a2538918.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论