I have here a simple program that displays random images that are in order, with two buttons that enable you to switch from an ascending order and then descending order. Now, I want to implement a random button that will display a random image of the selection but, I can't seem to figure out how. Below is my code:
var images = [
"1.jpg",
"2.jpg",
"3.jpg",
"4.jpg",
"5.jpg",
"6.jpg",
"7.jpg",
"8.jpg"
];
var num = 0;
function next() {
var slider = document.getElementById("slider");
num++;
if (num >= 8) {
num = 0;
}
slider.src = images[num];
}
function prev() {
var slider = document.getElementById("slider");
num--;
if (num < 0) {
num = 7;
}
slider.src = images[num];
}
function random() {
//where the code is supposed to be
}
<head>
<title>Laboratory Exercise 7</title>
</head>
<body>
<div>
<button onClick="prev()"> <--- </button>
<img id="slider" SRC="1.jpg" width="200px" height="100px" />
<button onClick="next()"> ---> </button>
<button onClick="random()"> Random </button>
</div>
</body>