Friday, May 2, 2014

Fun with Javascript

I was doing a programming assignment for Coursera and this entailed reading large amounts of information from a text file to be downloaded from the site. One could have used any other language to read the file and convert the data into an array. Since I've been doing web development for quite a while I used HTML and Javascript.

The logic was easy enough to encode but the hard part was converting the data. I settled for pasting the data into a div with an id so that I could reference the element using the DOM. I got the text using textContent, and then split the text - since the file had an item of data per row in the file, I used the newline as a separator parameter in the function invocation.

The first item of the data had to be inline with the opening div tag, and the last item of the data with the closing div tag. This resulted in an array of strings, which caused problems for me during debugging until I realized that the values needed to be parsed into the appropriate data type.

I checked out my implementation against the code that others have made online, and it's interesting how mine differed; some were shorter or made use of language-specific conventions. Others were more elegant. But all worked, just not equally in efficiency. 

It made me think of the martial arts I practice, and how the same moves can be found in other arts. Each variation hopefully having a purpose.



No comments:

Post a Comment