Reply: beginning-to-understand-javascript-a-bit

Blimey, what a great end-of-year gift I have received.


A remaining big question, though, is this: the Prof. draws the chart inside the function he created. I got stuck because I thought I had to create the data objects in a function and then return them to the chart.

We could write the tableToChart to return just the object and plug that into the chartist function call like Chartist.Line('.ct-chart', tableToObject(document.getElementById('table-source')), base_design); with tableToObject just returning the {"labels":..., "series":...} object, perfectly valid. I wanted to wrap it all up in one function but if you're using the data in other places then definitely compartmentalise that tableToObject functionality in its own ... function. You'd have to be careful to always have the function tableToObject return an object in the format Chartist.Line could process, as in the above function call it is always executed. I don't know if it would choke if it received false.

So what exactly does the return true; do that is different from the return: false; that happens if the script cannot find the table?

With JavaScript, every function returns something. Even if you don't return it returns 'null'. You can try this out using the browser console. If you right click on any browser window and go "Inspect Element" it should pop up a new window showing you the DOM-tree in a folder-like structure. If you move to "Console" you can then run any JavaScript function - including the new ones we make. You'll see that calling every function, indeed running every command, returns something. So I've tried to get in the habit of returning True in functions that don't actually return anything to give it a 'truey' value if it succeeded. Since null frequently equates to false, I figure returning true for success is sensible.

Honestly thinking further, I should have returned the new Chartist.Line object the function creates created so we could do more with it. Code evolves!