Algorithms and Data Structures · Junior · Question 70 of 75
Why should you avoid new Array(3).fill([]) for a 3 × 3 grid?
const grid = new Array(3).fill([]);
grid[0].push(1);- 1The grid ends up with 9 rows instead of 3
- 2
fillonly accepts numbers and strings - ✓Correct answer: All three rows are the same shared array
Explanation
fill puts the same object reference into every slot, so grid[1] and grid[2] also get the 1. Create separate rows instead: Array.from({ length: 3 }, () => []).