PDA

View Full Version : building a for loop



chriskq
05-14-2008, 11:51 PM
Hi, its been a while since i've attempted to do some js, and i am rusty.

heres the beginning of my loop:


for (var i=0; i<=100; i==) {

}

the output i want:


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

chriskq
05-15-2008, 08:02 AM
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,

codeexploiter
05-15-2008, 08:11 AM
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



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.