WordPress converting my code

<script> tags go in the document head, not in the document body (or, if in the document body, they should be the very last thing before the closing </body> tag.

Besides, if you’re adding code of any sort in your post, you’re going to run into issues if you use the Visual editor, or, more likely, if you’re switching between the Visual and HTML editors.