My code works although I have no idea how

There's mistakes but it works. I don't get it.