The other day I answered a question that was quite poorly written that arguably did not include all the details that was needed to give a complete answer to it. If I remember correctly, the OP's code example contained more than one error. One of the errors, however, was quite obvious and my answer (as well as others) pointed out this error. This error was apparently the one that OP's problem in all was solved as he commented on the answers, saying thanks, and I believe that he accepted one of the answers.
My own answer received 5 upvotes (which to me is well above average :)) and obviously I was content with the outcome. Since the question was a bit too vague, it received a few downvotes and eventually was closed and finally deleted. Thus, the rep I gained for my answer was lost.
I am OK with this behavior of SO, of course, but at the same time it discourages me to answer poor questions in the future. Why spend efforts on a question that is likely to be deleted?
So, my question is, should I avoid answering questions that I feel might be closed or should I give it a try anyways if I feel that I can help? Does the moderators take into consideration if any answers help the OP before closing? I feel that trying must be better for the community than leaving the question to die without trying. And if I give it a try, what should I do when I see that the post is attracting downvotes? (I don't have the rep to cast delete/undelete votes). Should I edit and flag if it gets closed? What do others do?
(I am interested in general guidelines, not in this case particularly even thought it serves as example).