Created
February 4, 2021 11:30
-
-
Save brianzelip/0d81341b9573433abb9a086d96df7688 to your computer and use it in GitHub Desktop.
Roll your own Array.prototype.indexOf()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The long way to Array.prototype.indexOf(), written while making an image slide show | |
const images = [...document.querySelectorAll('[data-js="carousel-img"]')]; | |
const activeEl = images.filter((img) => img.classList.contains('active'))[0]; | |
const activeElIndex = images.reduce((acc, img, i) => { | |
return acc != undefined | |
? acc | |
: img.classList.contains('active') | |
? i | |
: undefined; | |
}, undefined); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment