I'm a self-learner of JS. I need a hand. I want to make a private project of a background-slider as a click event. Let's say I have 3 backgrounds in the image folder: bgr1.jpg, bgr2.jpg, bgr3.jpg. I can put them into an array and loop through them, but I want to loop through them one by one and I need to add the condition that when loop reaches the last one, it starts from element [0]. I know that for some it may be a piece of cake but I'm feeling stuck. I can only list the array items all at the same time, but can anyone show how to display them one by one. I would appreciate.
<button id="btn">Next</button>
const backgrounds = ["bgr1.jpg", "bgr2.jpg", "bgr3.jpg"];
const btn = documentQuerySelector("#btn");
btn.addEventListener("click", function() {
for(let i=0; i<backgrounds.length; i++) {
console.log(backgrounds[i]);
}
// document.body.style.backgroundImage = ... (?)
})