473,396 Members | 1,971 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

For Loop Help

A simple question - How do I convert

var image1, image2, image3, image4

To a for loop that changes the variable name so I don't have to list them,
Like

for (var i = 1; i <= 4; i++) {
var image??
}

Thanks for the help.
Sep 6 '05 #1
8 1290
"Craig L" <w1******@hotSPAMmail.com> wrote in message
news:48********************@comcast.com...
A simple question - How do I convert

var image1, image2, image3, image4

To a for loop that changes the variable name so I don't have to list them,
Like

for (var i = 1; i <= 4; i++) {
var image??
}

Thanks for the help.


Not quite sure what you're asking for....

<html>
<head>
<title>varImage.htm</title>
<script type="text/javascript">

var image1 = "a.gif";
var image2 = "b.gif";
var image3 = "c.gif";
var image4 = "d.gif";

// To a for loop that changes the variable name so I don't have to list
them,

for (var i=1; i<5; i++) {
alert(eval("image" + i));
}

// eval() is evil ...

</script>
</head>
<body>
</body>
</html>
http://blogs.msdn.com/ericlippert/ar.../01/53329.aspx
Sep 6 '05 #2
Thanks for the quick response. At this point I'm just trying to declare the
variables image1, image2, image3, etc. for use later in the script, rather
than typing var image1, image2, . . ., image 20, image21, etc. The "image" +
i code is getting close, I think. Hope this helps to clarify.

Craig

"McKirahan" <Ne**@McKirahan.com> wrote in message
news:Q-********************@comcast.com...
"Craig L" <w1******@hotSPAMmail.com> wrote in message
news:48********************@comcast.com...
A simple question - How do I convert

var image1, image2, image3, image4

To a for loop that changes the variable name so I don't have to list
them,
Like

for (var i = 1; i <= 4; i++) {
var image??
}

Thanks for the help.


Not quite sure what you're asking for....

<html>
<head>
<title>varImage.htm</title>
<script type="text/javascript">

var image1 = "a.gif";
var image2 = "b.gif";
var image3 = "c.gif";
var image4 = "d.gif";

// To a for loop that changes the variable name so I don't have to list
them,

for (var i=1; i<5; i++) {
alert(eval("image" + i));
}

// eval() is evil ...

</script>
</head>
<body>
</body>
</html>
http://blogs.msdn.com/ericlippert/ar.../01/53329.aspx

Sep 6 '05 #3
McKirahan wrote:
"Craig L" <w1******@hotSPAMmail.com> wrote in message
news:48********************@comcast.com...
A simple question - How do I convert

var image1, image2, image3, image4

To a for loop that changes the variable name so I don't have to list them,
Like

for (var i = 1; i <= 4; i++) {
var image??
}

Thanks for the help.

Not quite sure what you're asking for....

<html>
<head>
<title>varImage.htm</title>
<script type="text/javascript">

var image1 = "a.gif";
var image2 = "b.gif";
var image3 = "c.gif";
var image4 = "d.gif";

// To a for loop that changes the variable name so I don't have to list
them,

for (var i=1; i<5; i++) {
alert(eval("image" + i));
}

// eval() is evil ...

</script>
</head>
<body>
</body>
</html>
http://blogs.msdn.com/ericlippert/ar.../01/53329.aspx

What!!!

As you said eval() is evil... then why use it!

You can reference the var like this

window["image" + n].src

Andy

----------------------------------------------------------------------
http://km0ti0n.blunted.co.uk/blog
http://km0ti0n.blunted.co.uk/mozXPath
http://km0ti0n.blunted.co.uk
----------------------------------------------------------------------

Sep 6 '05 #4
Craig L wrote:
Thanks for the quick response. At this point I'm just trying to declare the
variables image1, image2, image3, etc. for use later in the script, rather
than typing var image1, image2, . . ., image 20, image21, etc. The "image" +
i code is getting close, I think. Hope this helps to clarify.

Craig

"McKirahan" <Ne**@McKirahan.com> wrote in message
news:Q-********************@comcast.com...
"Craig L" <w1******@hotSPAMmail.com> wrote in message
news:48********************@comcast.com...
A simple question - How do I convert

var image1, image2, image3, image4

To a for loop that changes the variable name so I don't have to list
them,
Like

for (var i = 1; i <= 4; i++) {
var image??
}

Thanks for the help.


Not quite sure what you're asking for....

<html>
<head>
<title>varImage.htm</title>
<script type="text/javascript">

var image1 = "a.gif";
var image2 = "b.gif";
var image3 = "c.gif";
var image4 = "d.gif";

// To a for loop that changes the variable name so I don't have to list
them,

for (var i=1; i<5; i++) {
alert(eval("image" + i));
}

// eval() is evil ...

</script>
</head>
<body>
</body>
</html>
http://blogs.msdn.com/ericlippert/ar.../01/53329.aspx


Then use this...

for (...){
...
window["image" + i].src = "whatever.jpg";
...
}
Sep 6 '05 #5
Andrew Scott wrote:
Craig L wrote:
Thanks for the quick response. At this point I'm just trying to
declare the variables image1, image2, image3, etc. for use later in
the script, rather than typing var image1, image2, . . ., image 20,
image21, etc. The "image" + i code is getting close, I think. Hope
this helps to clarify.

Craig

"McKirahan" <Ne**@McKirahan.com> wrote in message
news:Q-********************@comcast.com...
"Craig L" <w1******@hotSPAMmail.com> wrote in message
news:48********************@comcast.com...

A simple question - How do I convert

var image1, image2, image3, image4

To a for loop that changes the variable name so I don't have to list
them,
Like

for (var i = 1; i <= 4; i++) {
var image??
}

Thanks for the help.
Not quite sure what you're asking for....

<html>
<head>
<title>varImage.htm</title>
<script type="text/javascript">

var image1 = "a.gif";
var image2 = "b.gif";
var image3 = "c.gif";
var image4 = "d.gif";

// To a for loop that changes the variable name so I don't have to list
them,

for (var i=1; i<5; i++) {
alert(eval("image" + i));
}

// eval() is evil ...

</script>
</head>
<body>
</body>
</html>
http://blogs.msdn.com/ericlippert/ar.../01/53329.aspx


Then use this...

for (...){
...
window["image" + i].src = "whatever.jpg";
...
}

Sorry I meant :

window["image" + i] = "whatever.jpg";

alert( image0 );
Sep 6 '05 #6
"Craig L" <w1******@hotSPAMmail.com> writes:
var image1, image2, image3, image4
If you have any serious number of such variables, you should probably
just use an array:

var image = [];
To a for loop that changes the variable name so I don't have to list them,
Like

for (var i = 1; i <= 4; i++) {
image[i] = ...
}


/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Sep 6 '05 #7
Lee
Craig L said:

Thanks for the quick response. At this point I'm just trying to declare the
variables image1, image2, image3, etc. for use later in the script, rather
than typing var image1, image2, . . ., image 20, image21, etc. The "image" +
i code is getting close, I think. Hope this helps to clarify.


var image= [ "a.jpg", "b.jpg", "c.jpg", "d.jpg" ];
alert(image[2]);

Sep 6 '05 #8
Thanks everyone for the help. I have it working. The array suggestion was
great since I do have a lot, hence the for loop coding.

Craig
"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:4q**********@hotpop.com...
"Craig L" <w1******@hotSPAMmail.com> writes:
var image1, image2, image3, image4


If you have any serious number of such variables, you should probably
just use an array:

var image = [];
To a for loop that changes the variable name so I don't have to list
them,
Like

for (var i = 1; i <= 4; i++) {


image[i] = ...
}


/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors:
<URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'

Sep 6 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Kingdom | last post by:
I Need some serious help here. strugling novis with ASP and javascript any help would be greatly appreciated The script below does exactly what I want it to do for each product on the two passes...
0
by: Tim::.. | last post by:
Hi can someone please give me some help with this little problem I am having with the following loop ...:: CODE ::. < 'Load XM set xml = Server.CreateObject("Microsoft.XMLDOM" xml.async = fals...
3
by: Gustavo Randich | last post by:
The following seems to be a bug. The execution returns rows 1,2. It should return 1,1. In fact, if I run the code within a stored procedure alone (not in a trigger), the loop doesn't overwrite the...
6
by: Shill | last post by:
I have several questions. In C, AFAIU, a for loop is just syntactic sugar for a while loop. for (i1; i2; i3) i4; is equivalent to i1 while (i2) {
8
by: Shamrokk | last post by:
My application has a loop that needs to run every 2 seconds or so. To acomplish this I used... "Thread.Sleep(2000);" When I run the program it runs fine. Once I press the button that starts the...
32
by: cj | last post by:
When I'm inside a do while loop sometimes it's necessary to jump out of the loop using exit do. I'm also used to being able to jump back and begin the loop again. Not sure which language my...
16
by: Claudio Grondi | last post by:
Sometimes it is known in advance, that the time spent in a loop will be in order of minutes or even hours, so it makes sense to optimize each element in the loop to make it run faster. One of...
3
by: Akira | last post by:
I noticed that using foreach is much slower than using for-loop, so I want to change our current code from foreach to for-loop. But I can't figure out how. Could someone help me please? Current...
8
by: SaltyBoat | last post by:
Needing to import and parse data from a large PDF file into an Access 2002 table: I start by converted the PDF file to a html file. Then I read this html text file, line by line, into a table...
4
by: joaotsetsemoita | last post by:
hello everyone. Im trying to time out a loot after a certain time. Probably 5 to 10 minutes. I have the following function Private Sub processFileCreation(ByVal source As Object, ByVal e As...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.