There are times when I write some code and it just works. Other times, I have to debug and refactor and iterate until it mostly works, then fine the edge cases and handle them. Then there are times when it just doesn't work. This happens, more often than I would like. Of course, this doesn't mean I'm a bad programmer (at least I hope it doesn't), but these things happen. Programming is a very complex subject, and even more complex to execute. The human brain isn't really set up for this kind of linear logic. Of course, we can train ourselves to think the way a computer might "think," but we can never fully function like a computer.
So sometimes you may want to bang your head against the keyboard. Sometimes no matter how much you try it just doesn't want to do things right. Sometimes it just doesn't work. And that's totally OK.