Collapse elements in a repeater with Null entry in database

This has got to be one of the frustrating forums to go on for answers. As I do not know of any other, I’d have to endure and be patient. Again, how is it that after a year, no one has posted an explanation as to why the codes recommended on this thread work for everything else except for galleries? Empty/undefined/null galleries don’t seem to get detected properly/consistently.