Skip to content

Instantly share code, notes, and snippets.

@brianzelip
Created February 4, 2021 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianzelip/0d81341b9573433abb9a086d96df7688 to your computer and use it in GitHub Desktop.
Save brianzelip/0d81341b9573433abb9a086d96df7688 to your computer and use it in GitHub Desktop.
Roll your own Array.prototype.indexOf()
// 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