0
Can you tell me the explanation about code in below? Especially the value of num when num--(start)
var images = [ 'http://www.sololearn.com/uploads/slider/1.jpg', 'http://www.sololearn.com/uploads/slider/2.jpg', 'http://www.sololearn.com/uploads/slider/3.jpg' ]; var num = 0; function next() { var slider = document.getElementById('slider'); num++; if(num >= images.length) { num = 0; } slider.src = images[num]; } function prev() { var slider = document.getElementById('slider'); num--; if(num < 0) { num = images.length-1; } slider.src = images[num]; }
1 Answer
+ 3
// Declare and initialize variable
// <nums> with zero
var num=0;
function prev() {
var slider = document.getElementById('slider');
// Decrement value of <nums>
num--;
// If value of <nums> less than zero
// let <nums> value be the last index
// of array <images>
if(num < 0) {
num = images.length-1;
}
// Set the image source for the img
// element <slider> to the URL
// stored in array <images> at index
// <num>
slider.src = images[num];
}
P.S. Please add JavaScript to Relevant Tags.
(Edit)
OP changed the embedded code.