From the description of the question, I think it was quite clear that the code doesn't work as intended.
As such, the code was not ready for review here,
and the question was likely to get closed.
In such case, users should not post an answer, but vote/flag to close instead.
Even if you think you can teach valuable lessons. The question is just not ready. You can always teach valuable lessons after OP whips it in shape.
You did answer it, so now what.
You have put work in that answer and I'd hate to see that wasted.
So I don't think it should be removed.
If it gets cleaned up by the system at some point,
for example due to many downvotes or something, so be it.
But I don't think we should take an explicit action right now.
The ideal outcome might be the OP fixing his problem,
and making the minimum amount of changes to the question to bring it on-topic,
and without invalidating too much of your answer.
But that may never happen.
Or the code may not be fixable without invalidating substantial parts of your answer.
Too bad then, and this is precisely why you should not review code that's not ready.