Javascript Help.

markjrubin

Golden Member
Jan 17, 2000
1,419
0
0
I've got the following Javascript code to display random images that doesn't want to work just right. It only loads a random image from the first 10 spaces on the array. I'm pretty darn sure it's because of the rand function only returning values from 0 to 1 (or is it 1 to 10?). Can anyone make this work for me? It's for a friend and I don't want to tell him I'm a retard and can't figure it out.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>Untitled</title>
<script language="JavaScript">
function Roll_Die(DSize){
var DRoll=0;
for (I=0;I<DSize;I++){
DRoll+=Math.round(Math.random()*2);
} return DRoll
}

var MyImages=new Array();
MyImages[0]="head_1.jpg";
MyImages[1]="head_2.jpg";
MyImages[2]="head_3.jpg";
MyImages[3]="head_4.jpg";
MyImages[4]="head_5.jpg";
MyImages[5]="head_6.jpg";
MyImages[6]="head_7.jpg";
MyImages[7]="head_9.jpg";
MyImages[8]="head_10.jpg";
MyImages[9]="jay-z_header.jpg";
MyImages[10]="ja-rule_header.jpg";
MyImages[11]="ashanti_header.jpg";

function RandomPix(){
var P=Roll_Die(MyImages.length);
alert (MyImages[P]);
//document.images[0].src="/images/headers/"+MyImages[P];
}

RandomPix();

</script>
</head>

<body>

<img border="0" src width="774" height="120">

</body>
</html>
 

ronopp

Senior member
Dec 2, 2002
291
14
81
did you get the teddybear virus letter and delete it? that is a debugging program for java SCRIPT??
 

stndn

Golden Member
Mar 10, 2001
1,886
0
0
hmmm... that is a strange randomizer function you have ...
but anyways ...

DRoll+=Math.round(Math.random()*2);
that will give you a random number between 0 and 2
and it seems like you add the numbers up to DRoll, and make that your random number instead (for the array index)
it will work just fine, but you have to be lucky to get the sum of DRoll to go above 10 to display the images from the later arrays...

why not just do this instead?
var P = Math.floor (Math.random() * (MyImages.length - 1));

that will give you a random number between 0 and 11, which will be the array index you can use

and just a suggestion, your code will be much easier to read if you put the functions in one place together and calls and operations at another place, instead of putting some on top of the code, then outside code, then function again -- just put the initializations and function calls above the script, then put the function declarations all below, following the rest ....
 

Softballslug

Senior member
Feb 22, 2000
397
0
0
The following page creates a random 800 x 100 header which is comprised of 2 images blended together...
==============================================

<html>
<head>
<title>Vacation Database II</title>
<SCRIPT LANGUAGE="JavaScript">
<!--
if ((screen.width < 800) || (screen.height < 600)) alert("This site must be viewed at 800x600 or higher");
function newPicture()
{
var image = new Array(
"/docproc/vacdb/images/EmpVacDB.jpg",
"/docproc/vacdb/images/EmpVacDB2.jpg",
"/docproc/vacdb/images/EmpVacDB3.jpg",
"/docproc/vacdb/images/EmpVacDB4.jpg",
"/docproc/vacdb/images/EmpVacDB5.jpg",
"/docproc/vacdb/images/EmpVacDB6.jpg",
"/docproc/vacdb/images/EmpVacDB7.jpg",
"/docproc/vacdb/images/EmpVacDB8.jpg",
"/docproc/vacdb/images/EmpVacDB9.jpg"
);
var num = Math.floor(Math.random() * image.length);
var imageSource = "<img src="+image
  1. +" width = 145 height = 100>";
    var gifSource = "<img src=/docproc/vacdb/images/VacDBTitle.gif width = 480 height = 100>";
    document.write(imageSource);
    document.write(gifSource);
    }
    // -->
    </SCRIPT>
    </head>
    <body bgcolor="#eaebef">
    <SCRIPT Language='JavaScript'>
    <!--
    newPicture();
    //-->
    </SCRIPT>

    *** REST OF THE HTML ***