Break up this jQuery code

This is my first post for S \ O and I am trying to figure out the following code and why it works, so I can better understand how to implement my own code.

I'm going through the Odin project and I'm stuck with creating a 16 x 16 grid. I want to look at an example but don't know how it 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 mesh?

Thanks in advance.

+3


source to share


1 answer


The last two lines simply reset the width and height of each DIV with the "grid" class to 640 divided by the number of rows / columns the grid creates.



Presumably the bit of code for setting the height / width could have been included right after the .append () call, but that's a matter of personal preference.

+4


source







All Articles