Results 1 to 2 of 2

Thread: Problem with for loop in javascript

  1. #1
    Join Date
    Jul 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with for loop in javascript

    Hi,

    I am developing the google application. In my google.js file consists the
    Code:
            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
    Code:
    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

  2. #2
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    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:
    Code:
    var icon = [];
    for(var i = 0; i < 5; i++){
        icon[i] = ...;
    }
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •