First, no one is expected to answer with perfectly working code. Anyone who insists that you provide perfectly working code is most likely a help vampire, and their question should be reviewed for edits to improve it or ultimate closure. However, the code you do put in your answer should, in my opinion, use good coding practices. Sometimes if you show someone the wrong way to do something, it's harder to get them to change it later on, once they gain experience and become more opinionated about their perceived skill level.
Second, your answer should also answer the question fully.
I believe you gave an answer that you felt would solve the problem, and your intentions were good. You also answered the question fully. However, I tend to be more of a purist. Mixing code is bad, unreadable, and should be avoided. I wouldn't have downvoted your answer, but I wouldn't upvote either.
Ultimately, some members of the community may feel your answer is helpful and upvote, while others may feel it's harmful and may downvote. People are free to vote as they choose, as long as it's on the content.
From looking at some of your answers, you're a good Stack Overflow user who writes detailed answers that will be useful to future visitors. Keep doing what you're doing. If you get a downvote and the downvoter leaves feedback, try to use that information to improve your answer if you can. If you can't really improve it, then just take it as a learning experience. Everyone gets a downvote occasionally, and sometimes these are the cases where we, as answerers, learn something. ;)
So don't let 1 downvote discourage you. Your answer might get upvoted if someone else thinks it's useful. Good luck!