Strictly speaking, to provide an answer to a question with bad formatting / lack of context does not break any rule. If you suggested such user to improve his/her question you did the right thing. The same applies to the downvote / closing vote, if you thought that was the right course of action.
But.
We all like MSE to be populated by good questions and great answers, and it is a duty of the whole community to grant a certain level of overall quality. So, even if answering a bad question is not a capital sin, in the long run it might contribute to lower the overall quality, by spreading the wrong idea that it is fine to dump a homework or a bad question on MSE, since, context or not, it will be answered anyway. Long story short: it is a judgement call, but it is probably better$^{(*)}$ to wait for the OP to improve his/her question, to give a hand if you like it, then to answer.
$(*)$ I would be a hypocrite in being too hard on you, since I behaved in the exact same way in many past occasions, especially during my long hunt for reputation. Then someone pointed out my behaviour was probably running against the interest of the community, and I have to share that she was probably right.