By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,760 Members | 1,557 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,760 IT Pros & Developers. It's quick & easy.

HELP needed, pls read inside

P: n/a
hello group,

I have a long (large) script that shows a random picture when a
webpage is refreshed. It's long because i have a lot of pictures: 246

Here is some code:
-----------------------------------------------------------------------------------------------

<!--
image1="smallpics/001-smallpic.jpg"
alt1="Ugly little picture!"

image2="smallpics/002-smallpic.jpg"
alt2="Ugly little picture!"

<<<you get the idea???>>>

image244="smallpics/244-smallpic.jpg"
alt244="Ugly little picture!"

image245="smallpics/245-smallpic.jpg"
alt245="Ugly little picture!"

image246="smallpics/246-smallpic.jpg"
alt246="Ugly little picture!"

len=246

now=new Date()
now=now.getSeconds()
rnd=now%len
image=eval("image"+rnd)
alt=eval("alt"+rnd)
document.write("<img src='" + image + "' alt='" + alt + "'
border=0></a>")
//-->

-----------------------------------------------------------------------------------------------

Can someone make it shorter for me?
The number of pics will grow and grow and i don't want to
copy/paste/adjust the [image***=] every time.
The pics are in seperate map on server.
The server does not support php :-(

Hope to hear from you.

tia
kind regards,
Buchw@ld
Feb 19 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Buchwald said the following on 2/19/2006 12:53 PM:
hello group,

I have a long (large) script that shows a random picture when a
webpage is refreshed. It's long because i have a lot of pictures: 246
Its long because you wrote it wrong. It is also bad code.
Here is some code:
-----------------------------------------------------------------------------------------------

<!--
If all the pictures are named as you have them, the image# variable is
not needed.

image###="smallpics/###-smallpic.jpg"

Is the format for the image name. You can generate that without having
to define it 246 times.

Then you create an Array to hold the alt text:

var altText = new Array();
altText['001'] = "Alt text for image 001";
altText['002'] = "Alt text for image 002";

....
altText['245'] = "Alt text for image 245";
altText['246'] = "Alt text for image 246";


len=246
len now equals altText.length, no need to hard-code it.

now=new Date()
now=now.getSeconds()
rnd=now%len
function Random(x) { return Math.floor(x*Math.random()) }
rnd = Random(altText.length);

image=eval("image"+rnd)
eval, and it's use, especially in this case, is indicative of bad code.
It's not needed.

imageRef = window['image' + rnd];

But, it is not needed.
alt=eval("alt"+rnd)
ditto.
document.write("<img src='" + image + "' alt='" + alt + "'
border=0></a>")
document.write('<img src="img'+rnd+'smallpics/'+rnd+'-smallpic.jpg"
border="0" alt="'+altText[rnd]+'">');

all on one line.

-----------------------------------------------------------------------------------------------

Can someone make it shorter for me?


var altText = new Array();
altText['001'] = "Alt text for image 001";
altText['002'] = "Alt text for image 002";
.......
altText['245'] = "Alt text for image 245";
altText['246'] = "Alt text for image 246";

function Random(x) { return Math.floor(x*Math.random()) }
rnd = Random(altText.length);
document.write('<img src="img'+rnd+'smallpics/'+rnd+'-smallpic.jpg"
border="0" alt="'+altText[rnd]+'">');

But, all of that would be better done on the server.
Now, all you have to do is add an altText'###' definition.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 19 '06 #2

P: n/a
JRS: In article <fM********************@comcast.com>, dated Sun, 19 Feb
2006 13:20:43 remote, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :

var altText = new Array();
altText['001'] = "Alt text for image 001";
altText['002'] = "Alt text for image 002";
......
altText['245'] = "Alt text for image 245";
altText['246'] = "Alt text for image 246";

function Random(x) { return Math.floor(x*Math.random()) }
rnd = Random(altText.length);
document.write('<img src="img'+rnd+'smallpics/'+rnd+'-smallpic.jpg"
border="0" alt="'+altText[rnd]+'">');

But, all of that would be better done on the server.
Now, all you have to do is add an altText'###' definition.

Was that tested?

There seems to be extra material after src= and the indices should
perhaps be not strings '001' to '246' but numbers 0 to 245 (or add one
to rnd) and rnd may then need extending to three digits.

Question :
The FAQ has that code for generating a random Number in 0..N-1 ; from
that one can build a random string of M digits representing 0..N-1. Is
there a better way, and if so might it be worth having in the FAQ?

S = String(Random(N)+1e10).substr(11-M)

( String.substr(-M) does not work for me)
OP : please use an informative Subject line.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Feb 19 '06 #3

P: n/a
Dr John Stockton said the following on 2/19/2006 6:17 PM:
JRS: In article <fM********************@comcast.com>, dated Sun, 19 Feb
2006 13:20:43 remote, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :
var altText = new Array();
altText['001'] = "Alt text for image 001";
altText['002'] = "Alt text for image 002";
......
altText['245'] = "Alt text for image 245";
altText['246'] = "Alt text for image 246";

function Random(x) { return Math.floor(x*Math.random()) }
rnd = Random(altText.length);
document.write('<img src="img'+rnd+'smallpics/'+rnd+'-smallpic.jpg"
border="0" alt="'+altText[rnd]+'">');

But, all of that would be better done on the server.
Now, all you have to do is add an altText'###' definition.

Was that tested?


Yes it was.

There seems to be extra material after src= and the indices should
perhaps be not strings '001' to '246' but numbers 0 to 245 (or add one
to rnd) and rnd may then need extending to three digits.
Indeed there is extra, unneeded, material after the src=. There are more
errors/problems than just that though.

If the random number is between 1 and 100 then the script will fail. It
needs a routine to pad it to three digits. Dropping the array index as a
string and using digits removes that problem.
Question :
The FAQ has that code for generating a random Number in 0..N-1 ;


The FAQ entry, to me, is misleading.

4.22 How do I generate a random integer in [1..N]?

function Random(x) { return Math.floor(x*Math.random()) }

Implies that Random gives that 1...N range. Even though it says in the
next sentence that it returns 0...(N-1).

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 20 '06 #4

P: n/a
JRS: In article <3-******************************@comcast.com>, dated
Sun, 19 Feb 2006 23:48:14 remote, seen in news:comp.lang.javascript,
Randy Webb <Hi************@aol.com> posted :
Question :
The FAQ has that code for generating a random Number in 0..N-1 ;


The FAQ entry, to me, is misleading.

4.22 How do I generate a random integer in [1..N]?

function Random(x) { return Math.floor(x*Math.random()) }

Implies that Random gives that 1...N range. Even though it says in the
next sentence that it returns 0...(N-1).

It's not the _next_ sentence; and ISTM that the part which you did not
quote explains, for those who need it, about adding 1. Its full
wording, references apart, is :-
4.22 How do I generate a random integer in [1..N]?

function Random(x) { return Math.floor(x*Math.random()) }

gives a random number in the range 0..(x-1); Random(N)+1 for [1..N]
But you could suggest that " use" should be inserted after the
semicolon.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Feb 20 '06 #5

P: n/a
Dr John Stockton said the following on 2/20/2006 5:42 PM:
JRS: In article <3-******************************@comcast.com>, dated
Sun, 19 Feb 2006 23:48:14 remote, seen in news:comp.lang.javascript,
Randy Webb <Hi************@aol.com> posted :
Question :
The FAQ has that code for generating a random Number in 0..N-1 ; The FAQ entry, to me, is misleading.

4.22 How do I generate a random integer in [1..N]?

function Random(x) { return Math.floor(x*Math.random()) }

Implies that Random gives that 1...N range. Even though it says in the
next sentence that it returns 0...(N-1).

It's not the _next_ sentence;

My apologies King John. But anybody with common sense wouldn't have been
confused by what I said.
and ISTM that the part which you did not quote explains, for those
who need it, about adding 1. Its full wording, references apart, is :-
4.22 How do I generate a random integer in [1..N]?

function Random(x) { return Math.floor(x*Math.random()) }

gives a random number in the range 0..(x-1); Random(N)+1 for [1..N]
But you could suggest that " use" should be inserted after the
semicolon.


The entry, and its explanation, is misleading. Think what you want.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 23 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.