+ 5

[solved ✔️] Image slider || onclick can you please simplyfi the second function prev()..

/* first function next() is easy to understand but the second one prev() is getting difficult to me please help me to simply the Prev() function*/ 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]; }

7th Apr 2020, 7:29 AM
⁣𓆩 Anкΐτ soℓคnкΐ ♣️
⁣𓆩 Anкΐτ soℓคnкΐ ♣️ - avatar
3 odpowiedzi
+ 5
What's the issue 🤔?.. The code seems correct.. Regarding the explanation about second function.. When you run the prev function the previous image would be displayed.. Now num is decremented and if num<0(which means there's no image behind and you're on first image-img 1) so you set num to images.length-1 i.e your final image in the array.. So if you run prev function when num is 0(that is you're at first image-img 1-then the previous image would be the last image in the array-img 3
7th Apr 2020, 8:01 AM
$hardul B
$hardul B - avatar
+ 4
$hardul B thanks for clarify the function Yes Code is correct but before your answer it was difficult to me how prev() function work.. thanks again
7th Apr 2020, 8:12 AM
⁣𓆩 Anкΐτ soℓคnкΐ ♣️
⁣𓆩 Anкΐτ soℓคnкΐ ♣️ - avatar
+ 2
Well imagine your gallery..when you go behind the very first image you see the last image 👍
7th Apr 2020, 8:50 AM
$hardul B
$hardul B - avatar