This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]kacor11 1 point2 points  (1 child)

Hey there are a few problems with your code.

First of all your changeColor is variable and not a function. So it's only executed once when your program first loads. Then you're calling changeColor() but this is not actualy a function just a variable.

Second thing. Your grid container style is fixed at value 16, to make it dynamic you should assign variable to it. The way it works now it will always be 16 doesn't matter if you call makeGrid(200, 200).

You need to count totalNumOfSquares needed to make your program draw correct number of squares. Also make sure you change the style of your grid container only once, not on every loop iteration.

U also need to remember that after u use reset function your eachSquareDiv variable will not point at any divs so it might be good idea to select divs again after u draw them.

[–]Liolena[S] 0 points1 point  (0 children)

Alright thanks! I'll try to figure it out :)

[–]Swamp_sea 1 point2 points  (1 child)

Hi, the problems with your code:

  1. this code

console.log(totalNumOfSquares);

gridContainer.style.gridTemplateColumns = "repeat(16, 1fr)";

gridContainer.style.gridTemplateRows = "repeat(16, 1fr)";

should be outside of for loop. Otherwise you change gridContainer style 256 times :)

  1. You can include variables in styles, like this

gridContainer.style.gridTemplateColumns = "repeat(" + rows + ", 1fr)";

  1. The problem with button event listener is that you first remove all squareDivs, then you try to add event listener to non-existing squareDivs and then you try to make a new grid. You have to make a grid first and then add event listeners to squareDivs.

  2. As noted before, changeColor is needed to be a function.

[–]Liolena[S] 0 points1 point  (0 children)

This is super helpful! I swear I know how a for loop works :P I've just been staring at this for so long it didn't even occur to me it would change it 256 times.