HELP needed, pls read inside

B

Buchwald

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
 
R

Randy Webb

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 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.

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.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Sun, 19 Feb
2006 13:20:43 remote, seen in Randy Webb
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.
 
R

Randy Webb

Dr John Stockton said the following on 2/19/2006 6:17 PM:
JRS: In article <[email protected]>, dated Sun, 19 Feb
2006 13:20:43 remote, seen in Randy Webb
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).
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated
Sun, 19 Feb 2006 23:48:14 remote, seen in
Randy Webb said:
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.
 
R

Randy Webb

Dr John Stockton said the following on 2/20/2006 5:42 PM:
JRS: In article <[email protected]>, dated
Sun, 19 Feb 2006 23:48:14 remote, seen in
Randy Webb said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,077
Messages
2,570,566
Members
47,202
Latest member
misc.

Latest Threads

Top