phillip swag phillip swag - 2 months ago 20
HTML Question

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>

Answer

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