So Ive seen a number of programming questions answered here so Im going to post and hope for the best. Im stuck on a pretty simple javascript command, I am creating UI elements for an array and I want to add 5 units to the y height for each var in the iteration. Im aware of the ++ command but dont understand how its implemented into a system like this.
So heres what I have so far and I was hoping maybe someone could explain where and how its supposed to be written,
var i=5;
for(var a in list)
{
win.main.add('statictext', {x:15, y:15+i, width:470, height:70} , [a]);
i++;
}
Replies
var i=0; var offset=5; for(var a in list) { win.main.add('statictext', {x:15, y:15+i*offset, width:470, height:70} , [a]); i++; }var i=0; for(var a in list) { win.main.add('statictext', {x:15, y:15+i, width:470, height:70} , [a]); i+=5; }Should also work and helps show where the problem was in your code. You were only adding 1 to i each loop so the first iteration would add 5 the second 6 etc...I think you got confused between the 'for' loop and the 'for in' loop. For the 'for' loop you would do something like this.
for (i = 0; i < 5; i++) { text += "The number is " + i + "<br>"; }Where i controls the number of loops and so you increase i to keep the iterations going. For the 'for in' loop it will run as long as you as there are more variables in your list so the value of i doesn't control the number of iterations but in this case controls the amount of height added each iteration and therefore you need to add 5 to it for each loop.
As a final note both my solution above and Farfarer's will start with the first iteration not adding 5 to the height but the second iteration will. If this isn't what you want then change the i value to be 1 in Farfarer's code or 5 in the example above.
Final final note: Are you sure you want to be adding to the y property and not the height property? It would make sense that x,y are positions and width and height are the dimensions. In which case just take the +i or +i*offset and move it to the height value.
Or if you need to reference it elsewhere.