>but why is that the way to do it?
It is one way to do it because it works. This may not seem like a very useful answer to you now, though solving problems like these is often about try, error and hopefully you can see a pattern in certain problems and reuse the solution of previous solved problems.
Also keep in mind that this is surely not the only way, alot of other work too. Some may be less elegant than others, but that doesn't mean they are wrong. Your only goal is to find one working way, not necessarily the nicest, elegant or shortest one.