Results 1 to 3 of 3

Thread: building a for loop

  1. #1
    Join Date
    Apr 2007
    Location
    Sydney, Aust - Downunder
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default building a for loop

    Hi, its been a while since i've attempted to do some js, and i am rusty.

    heres the beginning of my loop:

    Code:
    for (var i=0; i<=100; i==) {
    
    }
    the output i want:

    Code:
    var my_staff_1 = new Tooltip ('trigger_1', 'staff_1')
    incrementing each number in the above until 100

    i don't want to document.write this anywhere so that people can see the output (the function is sitting just before the closing </body> tag), but i dont want to copy and paste that line of javascript and increment each number 100 times....

    any help appreciated.
    what book does someone recommend to someone who is struggling with javascript

  2. #2
    Join Date
    Apr 2007
    Location
    Sydney, Aust - Downunder
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    nearly 30 views, but no responses.
    Is my question difficult to understand (or the output i want)
    Or is it something trickey to create, as why to no responses. (in my head it seems simple enough, esp as im already doing it with php in my page - but maybe im missing something)
    once again - any feedback appreciated, Cheers,

  3. #3
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    If you are trying to create a method in which you need to instantiate 100 objects then I think there should be some points that needs to be considered.

    If you use 100 different variables for storing the instntiated object then it would be difficult to maintain them or if you just use a single variable for the object instantiation then the variable will hold only the last object instantiation. So I think it would be better if you use arrays for this purpose.

    Have a look at the following code

    Code:
    var objArray = new Array();
    for(var i = 0; i <= 100; i++){
    	objArray[objArray.length] = new Tooltip ('trigger_' + i, 'staff_' + i);
    }
    If you look at the parameter that you pass while calling the Tooltip constructor I've changed the section in a manner which appends the number (current value of i) at the end of 'trigger_' and 'staff_' strings. You can access any objects by accessing the element of the array using its index.

    Let me know if this is what you are looking for.
    Last edited by codeexploiter; 05-15-2008 at 08:21 AM. Reason: Correction in JS code

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
  •