Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
mryoung
4,156 PointsWhy is my for loop not reversing?
It just goes to the second slide (there are 3).
prev.addEventListener('click', function () {
for(let i = 0; i < slides.length; i--){
slides[i].style.display = 'none';
}
slideIndex--;
if(slideIndex > slides.length){
slideIndex = 1;
}
slides[slideIndex -1].style.display = 'block';
})
1 Answer
KRIS NIKOLAISEN
54,974 PointsWith this you have an infinite loop
for(let i = 0; i < slides.length; i--)
slides.length is positive and you are looping from 0 to negative infinity so the condition will never be met.
Are you trying to do something like this?
for(let i = slides.length; i > 0 ; i--)
mryoung
4,156 Pointsmryoung
4,156 PointsIām trying to loop it backwards so when a button is clicked my images will just return from the last one to the first one.