<!DOCTYPE html> <html> <body> <script type="text/javascript">

      var images = ["strawberry.jpg", "apple.jpg", "cherry.jpg", "orange.jpg", "pear.jpg"];


  var length = images.length;
  var randImg1 = Math.round(Math.random() * (length - 1));
  document.write('<img src="' + images[randImg1] + '" >');
  var randImg2 = Math.round(Math.random() * (length - 1));
  document.write('<img src="' + images[randImg2] + '" >');
  var randImg3 = Math.round(Math.random() * (length - 1));
  document.write('<img src="' + images[randImg3] + '" >');

  if (randImg1 == randImg2 && randImg2 == randImg3 && randImg3 == "strawberry.jpg") {

  }

</script> </body> </html>

I want to compare the images of the arrays, if 3 images are strawberry there should be an alert box like "congrats you won" how can I do that ?

Recommended Answers

All 4 Replies

Line 23 looks off. randImg are numeric so without me testing it, try

if (randImg1 == randImg2 == randImg3 && images[randImg3] == "strawberry.jpg") {

I think it worked but when the strawberry images are matched i cannot see the images on the screen , i just see the alert box message that i wrote "congrats you won".

Perils of using document.write. That queues up that to be done after your javascipt exits (well at least here.)

You may want to use other than document.write. Or change the alert to your document.open code.

try this to fix your images not loading and the alert checking system:

window.onload = function() {
    if (randImg1 == randImg2 == randImg3 && images[randImg3] == "strawberry.jpg") {
        alert("You Won!");
    }
};

Window.onload = function() {} basically says "When the document is totally and completely loaded, do this in the function." It will load all your images to the image box and THEN, when they are loaded, alert the user that they have won.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.