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
I think you got confused between the 'for' loop and the 'for in' loop. For the 'for' loop you would do something like this.
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.