you really only need the below code to get what you want quickly and simply
Index is the layer number
Add one for next layer number
refer to that layer with “thisComp.layer()”
and the .name prefix will return the name
Everything else just makes the code scaleable so that you can redeploy if in other projects.
But if its just the next layer you want, one time thing, just use the simple code.
To reply to the above your maths is right, and logic is right.
But the result is the other way around from what you have. If all are 0 then there is nothing visible and it will continue through the loop to find the first visible instance.
i=thisLayer.index+1;
n=thisComp.layer(i).name;