forEach method

i'm finding it difficult to understand this code snippet? could someone please tell me what the slide and index do? i tried to change the forEach method to a forLoop so i can understand it better, but i could not access the slide property. this is my attempt to rewrite it in forLoop method: for(index=0; index<slides.length; index++){ slide.style.transform = translateX(${index * 500}px); }
3 Replies
MarkBoots
MarkBootsβ€’3y ago
a foreach loop goes over an array. Inside the loop every item of that array will be temporarily be named as 'slide' and the index is the place of that item in the array, which you can refer too. here a more detailed explanation: https://www.freecodecamp.org/news/javascript-foreach-how-to-loop-through-an-array-in-js/ in a regular for loop it would be
for(let index = 0; index < slides.length; index++){
const slide = slides[index];
slide.style.transform = `translateX(${index * 500}px)`
}
for(let index = 0; index < slides.length; index++){
const slide = slides[index];
slide.style.transform = `translateX(${index * 500}px)`
}
Unknown User
Unknown Userβ€’3y ago
Message Not Public
Sign In & Join Server To View
winniffy
winniffyOPβ€’3y ago
preciate this a lot @MarkBoots thank you @webcat || 🧢🐈 you both helped a lot. preciate it
Want results from more Discord servers?
Add your server