LightGallery is counting wrong number of images.. here is jsfiddle code... In this jsfiddle i am append dynamical element with a div that is in html file.. Please check manually ... Why lightgallery is count wrong images ?
If you have 5 items and then loop 0 to 5. That's one too many.
Your choice of -1 or change i<= to i< whichever you can wrap your head around. The right way would be to drop the = sign but the key item is for you to think of the array count is one number and arrays start at zero so think about that.