We came across this problem recently whilst developing our post on the TF-IDF function. The solution was simple:
Whether you put the reference in the head or bottom of the body depends on whether or not you need the script to have been loaded before or after the page has finished loading. If it is critical in some way to the way the page loads then put it in the head, otherwise put it at the bottom. Putting it in the head will slow the page load speed.
You can type the code directly into scripts and styles, which would mean that you didn’t have to work with a local file on the server, but we try to use elm for adding custom features to our site because of its simplicity and incredible reliability, which necessitates adding a file.