Welcome to the forum - great to have you here.
Nice solution! Thanks for sharing it with the forum.
If you’re interested, another potential approach might be to do this as a recursive M function. In her epic writeup for Challenge #5, @Melissa provides the code for a recursive function in a somewhat similar context.
- Brian