PapiP
PapiP

Reputation: 47

Why wont my random image generator show?

My random images wont display. I even ran it through a html validator and still cant seem to find the error.

function showImg() {
        /*
            The showImg() function displays a random image from the 0.png through 9.png files. The random image is designed to thwart hackers attemting to enter the library records database by requiring visual confirmation.

        */

        var imgNumber = randomInteger(9):// Return a random number from 0 to 9
        document.write ("<img src='" + imgNumber + ".png' alt=' ' />");
        }




      </script>



<script type="text/javascript">
                    showImg();
                    showImg();
                    showImg();
                    showImg();
                    showImg();
                </script>

Upvotes: 0

Views: 77

Answers (2)

Kate
Kate

Reputation: 1836

Are your images stored in the same folder as the page you're calling ? If they are in a subfolder then you obviously need to specify the path in your code - either relative or absolute.

Upvotes: 0

guest271314
guest271314

Reputation: 1

Adjust showImg to

function showImg() { 
  var imgNumber = randomInteger(9);// Return a random number from 0 to 9 
  document.body.innerHTML += "<img src=" + imgNumber + ".png alt='' />"; 
}

for randomInteger function you can use

function randomInteger(n) {
  return Math.floor(Math.random() * (n - 0 + 1)) + 0
}

See also Math.random()

plnkr http://plnkr.co/edit/VTaRXAsXoAiwVTiy7yh7?p=preview

Upvotes: 1

Related Questions