473,395 Members | 1,763 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,395 software developers and data experts.

HELP needed, pls read inside

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
5 1187
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: PHPkemon | last post by:
Hi there, A few weeks ago I made a post and got an answer which seemed very logical. Here's part of the post: PHPkemon wrote: > I think I've figured out how to do the main things like...
0
by: Bruce E. Pullum | last post by:
Ok, I really need some help here.... I have an ASPX page, that contains a save button. It also has an IFRAME that has a tabstrip and multipage control. What I am trying to do is to allow the...
9
by: Julia Briggs | last post by:
How do I construct a <iframe> or equivalent for FireFox/NS browsers, inside a screen centered <div> tag? Can it be done?
2
by: Dave | last post by:
I hope someone can help i have a little understanding of Access and need to create a database that will allow me to keep track of employees work hours on various processes involved in building...
13
by: Chua Wen Ching | last post by:
Hi there, I saw this article here in vb.net. http://www.error-bank.com/microsoft.public.dotnet.languages.vb.1/148992_Thread.aspx and ...
0
by: john | last post by:
Hi,All MS Tech Gurus: I need your help!!! It is the second time I post this message, I need get some feedback ASAP, Please Help!! Thanks a lot in advance. John I have a csharp method, using...
8
by: rh0dium | last post by:
Hi all, I am using python to drive another tool using pexpect. The values which I get back I would like to automatically put into a list if there is more than one return value. They provide me...
6
by: JOSII | last post by:
Getting a string of boolean value into and out of the registry is no problem. Here's the problem: Although you can place an object into the registry and retreive it, I need to place an ArrayList...
3
by: Stephen Torri | last post by:
Below is a class that is suppose to represent a segment of memory or a contents of a binary image (e.g. ELF executable). I have started to read Modern C++ Design and thought the best way to ensure...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.