Currently diving into the world of Javascript, I came across this interesting method to create a 16X16 grid using example.repeat(number) with a numerical value. While I have a general understanding of the code flow, I'm struggling to fully grasp how the 'repeat' function works in this context. Any insights would be greatly appreciated!
Check out the result on CodePen: https://codepen.io/shogunhermit15/pen/mdxyqMN?editors=1010
function buildGrid(x, y, cellSize, gridElement) {
gridElement.style.display = "grid";
gridElement.style.gridTemplateColumns = `repeat(${x}, ${cellSize}px)`;
gridElement.style.gridTemplateRows = `repeat(${y}, ${cellSize}px)`;
let squares = new DocumentFragment();
for (let i = 0; i < x * y; i++) {
let square = document.createElement('div');
square.className = 'square';
squares.appendChild(square);
}
gridElement.appendChild(squares);
}
buildGrid(16, 16, 25, document.querySelector(".grid"));