JavaScript Loop Sample For Discussion
There's More Than One Way To Do It
Completing any specific programming task is rarely limited to a single approach. Take rending a table on a web page. There's a zillion ways to do it. This page has one of them1.
The goal is to take data exported from an Excel document into a CSV format2 and render it on a web page3.
Talking About It
I'm not really getting into the details of the code for this post. It's primary purpose is as a sample to discuss in person.
Show Me The Code (Version 1)
Here's the full set of code for making parsing the input data and rendering the table.
The "Output" section below the code shows the results.
HTML
Name
HP
Attack
Defense
Sp. Atk
Sp. Def
Speed
Output
| Name | HP | Attack | Defense | Sp. Atk | Sp. Def | Speed |
|---|---|---|---|---|---|---|
| Bulbasaur | 45 | 49 | 49 | 65 | 65 | 45 |
| Ivysaur | 60 | 62 | 63 | 80 | 80 | 60 |
| Venusaur | 80 | 82 | 83 | 100 | 100 | 80 |
Adding Some Extras (Version 2)
This version of the code adds in some formatting. The HP column cells get a tag wrapped around them to make them bold. The key is this line which provides a counter to keep track of which cell is being worked on.
let currentCell = 1;It's placed outside this loop over the cells:
The currentCell variable is then used inside the loop to change the output of the HP cell to include the tag in the loop.
HTML
Name
HP
Attack
Defense
Sp. Atk
Sp. Def
Speed
Output
| Name | HP | Attack | Defense | Sp. Atk | Sp. Def | Speed |
|---|---|---|---|---|---|---|
| Bulbasaur | 45 | 49 | 49 | 65 | 65 | 45 |
| Ivysaur | 60 | 62 | 63 | 80 | 80 | 60 |
| Venusaur | 80 | 82 | 83 | 100 | 100 | 80 |
A Little Style
This little snippet of code adds some styling to the table to make it take up the full width of the content column and add a color to the cells.
There You Have It
That's it for the basics.
Of course, this doesn't get into how to update the data. That's a longer conversation for another post.
-a
Footnotes
This code is designed to be as beginner friendly as possible. It can be read from top down and doesn't introduce explicit or inline functions.
In the second version, the index counter for the for loop is done as an external variable to keep the two for instances consistent.
It also uses the + for string concatination. That's easier to understand than strings with backtics and inline variables.
Another thing is using let everywhere when const is what I'd use with more experience. Again, limiting to the single approach reduces the overall mental overhead.
The code is also designed to be the only thing on the page. The style sheet works directly on the table and td elements to prevent having to introduce and explain classes.
Depending on the context and the amount of time folks have it's worth getting into those topics. Their out of scope for this post.
The Excel to CSV export uses ; characters instead of commas.
Making a generalized CSV parser that can deal with the arbitrary CSV files is a complicated endeavour. It's best to use a pre-built library if that's your goal. The goal of this post is to show the entire process without external libraries.
Given that it's a single CSV file that resides with the code base doing the parsing this way works fine.
This is all done with "Vanilla JavaScript". That is, no external libraries of frameworks. You can copy/paste this code into a .html file and it'll work without having to fuss with anything else.