Edward Winch Edward Winch - 10 months ago 53
Javascript Question

Javascript function:return random picture

New to javascript. Im trying to code an external javascript function to randomly display a picture. There are 5 pictures are under a folder called "images/image1.jpg".

I have a couple alerts one called alert("test"); in the HTML and alert("me"); in the javascript external function these are not working as well. I don't know why this isn't working.

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<script src="scripts/random.js" type="text/javascript"></script>
<script type="text/javascript">
var randNumb;
randNumb = randomInteger(5);
//This script calls the external function randomInteger() to display random pictures
document.write("<img src="images/image" + randNumb + ".jpg" alt="Image" + randNumb");

//FUNCTION randomInteger()
function randomInteger(n) {
return Math.ceil(Math.random() * n);

Answer Source

Looks like this line is wrong:

document.write("<img src="images/image" + randNumb + ".jpg" alt="Image" + randNumb");

should be:

document.write("<img src='images/image" + randNumb + ".jpg' alt='Image" + randNumb + "'/>");