This is my first post to S\O and I'm trying to get my head around the following code and why it works so I can have a better understanding on how to implement my own code.
I'm going through the Odin Project and I'm stuck on creating a 16 x 16 grid. I choose to look at an example, but am unsure how it fully works.
Here's the code:
function createGrid(x) {
for (var rows = 0; rows < x; rows++) {
for (var columns = 0; columns < x; columns++) {
$("#container").append("<div class='grid'></div>");
};
};
$(".grid").width(640/x);
$(".grid").height(640/x);
};
I don't understand why the bottom two lines are needed. I thought the loop would be enough to create the grid?
Thanks in advance.
The last two lines simply reset the width and height of every DIV with a class of "grid" to 640 divided by the number of rows/columns the grid is creating.
Conceivably, that bit of code logic to set the height/width could have been included right after the .append() call, but this is a matter of personal preference.