beaucarnes / fcc-project-tutorials

freeCodeCamp video project tutorials.
290 stars 416 forks source link

Question about array scope in Game of Life #8

Open daveronson opened 6 years ago

daveronson commented 6 years ago

Great video about creating the Game of Life.

I was trying to enhance the gridSize function to call this.seed and then this.playButton after this.clear but I get an out of bounds error when choosing the largest grid because the array is still 50x30 and it's trying to seed 70x50. This obviously doesn't happen in the original code when you select gridSize and then separately click the seed button.

I'm not sure if this is state vs prop vars, an array creation issue or if this is a array scope issue? Any thoughts would be appreciated.

donkeypagoda commented 6 years ago

@daveronson hey you've probably already solved this, but I wanted to solve this problem and have it auto-seed as well. I tried moving the rows and cols into state, but I had the same failure as before when I added the this.seed() call after this.clear() in the gridSize() function. So I don't think it's a scope issue, maybe I'm wrong. But when I removed the arrayClone() call from the seed() function [the one doing the JSON stringify/parse trick] and replaced it with a newly generated gridCopy built of the this.row and this.col values, everything worked. In fact, you don't need to call this.clear() in the gridSize() at all, just call this.seed() Anyways, my repo of it auto-seeds, if you'd like to check it out: https://github.com/donkeypagoda/gol-react-tutorial