Edward Winch Edward Winch - 1 month ago 8
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">
<head>
<meta charset="UTF-8">
<title>Winch-Lab8</title>
<script src="scripts/random.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
var randNumb;
randNumb = randomInteger(5);
alert("test");
//This script calls the external function randomInteger() to display random pictures
document.write("<img src="images/image" + randNumb + ".jpg" alt="Image" + randNumb");
</script>
</body>
</html>

//FUNCTION randomInteger()
function randomInteger(n) {
alert("me");
return Math.ceil(Math.random() * n);
}

Answer

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 + "'/>");
Comments