0

i want to display random images with js, my code as follows

var imageList=new Array();
imageList[0]="img1.jpg";
imageList[1]="img2.jpg";
imageList[2]="img3.jpg";
imageList[3]="img4.jpg";
var imageChoice=Math.floor(Math.random*imageList.length);
document.write('<img src="'+imageList[imageChoice]+'"'  );

2 Answers2

0

random() is a method of the Math object. You're missing the brackets in order to call it as a function:

var imageChoice = Math.floor(Math.random() * imageList.length);

As a side note, I would suggest initializing your image array using square bracket notation like this:

var imageList = ["img1.jpg", "img2.jpg", "img3.jpg", "img4.jpg"];
Community
  • 1
  • 1
rink.attendant.6
  • 36,468
  • 57
  • 89
  • 143
0

Here is a sample select random color from one line of code!

[].forEach.call($$("*"),function(a){
  a.style.outline="1px solid #"+(~~(Math.random()*(1<<24))).toString(16)
})

check the detail from this link

Gencebay
  • 451
  • 2
  • 5
  • 12