Creating images sliders in JS | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Creating images sliders in JS

I'm trying to create a Previous and a Next button to change the image, the Next button works, but not the Previous button. The first code shown has something wrong that creates a problem which JS says is "Uncaught ReferenceError: prev is not defined" the second one is correct but I can't find a difference between these codes can anyone help, please? The HTML is correct the problem is probably somewhere in the JS Code 1 (incorrect): 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 perv() { var slider = document.getElementById("slider"); num--; if(num < 0) { num = images.length-1; } slider.src = images[num]; } Code 2 (correct): 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]; } */ Here is the link to the program the incorrect code is a comment

31st Jan 2020, 3:45 AM
Danial Azadpour
Danial Azadpour - avatar
3 Answers
+ 2
typo in function name (perv instead of prev) in version one
31st Jan 2020, 7:34 AM
Gordon
Gordon - avatar
+ 3
31st Jan 2020, 4:06 AM
BroFar
BroFar - avatar
- 1
Thank you, Gordon, for pointing that out!
2nd Feb 2020, 12:33 AM
Danial Azadpour
Danial Azadpour - avatar