Skip to content
Advertisement

How to replace a loop for a loop?

I have a grid made with a preset value that I’d like to have a function that replaces it with a grid who’s cells are generated with dynamic values gotten from a prompt input. The code for both grids work independently if I were to remove the other’s code, but for whatever reason I can’t get the 2nd grid to replace the first one when it gets it’s value.

Here is an example of my JS code that has the .replaceWith() method attempting to replace the cells of the original grid via div. I’m stumped.

JavaScript
JavaScript

Advertisement

Answer

As mentioned by @Bergi try using board.replaceChildren() to empty the board first first, and then refill the board like you do in the initial loop.

The cell.replaceWith does not do anything meaningful and can be removed. cell in this scope is the one you create in line 6 of your script, which you never append to the body or do anything else with.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement