PDA

View Full Version : Problem with for loop in javascript



swetha
07-16-2009, 05:40 AM
Hi,

I am developing the google application. In my google.js file consists the


icon1=new GIcon();
icon1.image = "images/bmw-small.png";
icon1.shadow = "images/shadow50.png";
icon1.iconSize = new GSize(25, 25);
icon1.shadowSize = new GSize(20, 19);
icon1.iconAnchor = new GPoint(10, 15);
icon1.infoWindowAnchor = new GPoint(9, 2);
icon1.infoShadowAnchor = new GPoint(18, 25);
icon2 = new GIcon();
icon2.image = "images/bmw-small.png";
icon2.shadow = "images/shadow50.png";
icon2.iconSize = new GSize(25, 25);
icon2.shadowSize = new GSize(20, 19);
icon2.iconAnchor = new GPoint(15, 20);
icon2.infoWindowAnchor = new GPoint(9, 2);
icon2.infoShadowAnchor = new GPoint(18, 25);
icon3 = new GIcon();
icon3.image = "images/bmw-small.png";
icon3.shadow = "images/shadow50.png";
icon3.iconSize = new GSize(25, 25);
icon3.shadowSize = new GSize(20, 19);
icon3.iconAnchor = new GPoint(20, 25);
icon3.infoWindowAnchor = new GPoint(9, 2);
icon3.infoShadowAnchor = new GPoint(18, 25);
icon4 = new GIcon();
icon4.image = "images/bmw-small.png";
icon4.shadow = "images/shadow50.png";
icon4.iconSize = new GSize(25, 25);
icon4.shadowSize = new GSize(20, 19);
icon4.iconAnchor = new GPoint(25, 30);
icon4.infoWindowAnchor = new GPoint(9, 2);
icon4.infoShadowAnchor = new GPoint(18, 25);
icon5 = new GIcon();
icon5.image = "images/bmw-small.png";
icon5.shadow = "images/shadow50.png";
icon5.iconSize = new GSize(25, 25);
icon5.shadowSize = new GSize(20, 19);
icon5.iconAnchor = new GPoint(30, 35);
icon5.infoWindowAnchor = new GPoint(9, 2);
icon5.infoShadowAnchor = new GPoint(18, 25);

Now i want to replace this with for llo condition .For that purpose i have changed that as


for(var i=1;i<=5;i++){
icon5 = new GIcon();
'icon'+i.image = "images/bmw-small.png";
'icon'+i.shadow = "images/shadow50.png";
'icon'+i.iconSize = new GSize(25, 25);
'icon'+i.shadowSize = new GSize(20, 19);
'icon'+i.iconAnchor = new GPoint(30, 35);
'icon'+i.infoWindowAnchor = new GPoint(9, 2);
'icon'+i.infoShadowAnchor = new GPoint(18, 25);
}


But this for llop condition is not working. Please anybody tell the whats the problem in my code and how can use to work the for loop condition.Please help me Its very urgent.



Thanks
Swetha

Jesdisciple
07-17-2009, 01:14 AM
String identifiers only work for object properties, not for ordinary variables. You might be able to access these variables as properties, but I can't know without the rest of the JS file.

Alternatively, if you can change the code which uses these variables you could make it allow for this:
var icon = [];
for(var i = 0; i < 5; i++){
icon[i] = ...;
}