If YES, stop throwing it away.
If NO, then you are not finished figuring the problem out. Keep iterating. The first time you write something that works, start working on another feature.
Once you're working on another feature, you have a choice to either abandon something that is not working yet in order to refactor something that already works, or keep going on the new feature and leave the old one alone. That should be an easy choice ;)
Engineering is largely about creating solutions that satisfy the requirements. Perfectly crafted code isn’t a requirement. As long as the code works, is maintainable, and is commented appropriately then you’re done.