[ '819..5.....2...75..371.4.6.4..59.1..7..3.8..2..3.62..7.5.7.921..64...9.....2..438', '53..247....2...8..1..7.39.2..8.72.49.2.98..7.79.....8.....3.5.696..1.3...5.69..1.', '..3.2.6..9..3.5..1..18.64....81.29..7.......8..67.82....26.95..8..2.3..9..5.1.3..', '394..267....3..4..5..69..2..45...9..6.......7..7...58..1..67..8..9..8....264..735', '97.3...6..6.75.........8.5.......67.....3.....539..2..7...25.....2.1...8.4...73..', '4......6.5...8.9..3....1....2.7....1.9.....4.8....3.5....2....7..6.5...8.1......6', '85...24..72......9..4.........1.7..23.5...9...4...........8..7..17..........36.4.', '..1..5.7.92.6.......8...6...9..2.4.1.........3.4.8..9...7...3.......7.69.1.8..7..', '.9...4..7.....79..8........4.58.....3.......2.....97.6........4..35.....2..6...8.', '12.3....435....1....4........54..2..6...7.........8.9...31..5.......9.7.....6...8', '9..2..5...4..6..3...3.....6...9..2......5..8...7..4..37.....1...5..2..4...1..6..9', '1....7.9..3..2...8..96..5....53..9...1..8...26....4...3......1..4......7..7...3..', '12.4..3..3...1..5...6...1..7...9.....4.6.3.....3..2...5...8.7....7.....5.......98', '..............3.85..1.2.......5.7.....4...1...9.......5......73..2.1........4...9', '.......39.....1..5..3.5.8....8.9...6.7...2...1..4.......9.8..5..2....6..4..7.....', '....839..1......3...4....7..42.3....6.......4....7..1..2........8...92.....25...6', '..3......4...8..36..8...1...4..6..73...9..........2..5..4.7..686........7..6..5..' ].forEach(reduceGrid); // Or of you want to create all the grids of a particular n-size. // I run out of stack space at n = 9 let n = 2; let s = new Array(Math.pow(n, 4)).fill('.').join(''); reduceGrid(s);