While working on a problem in Leetcode, I encountered an issue with an empty array. I attempted to push some numbers into it and received an error message that I am unsure of the reason for. Below is my code snippet:
// r and c values are pre-defined numbers, arr is also a pre-defined array.
let n = [[]]
let index = 0
for (let i = 0; i < r; i++) {
for (let j = 0; j < c; j++) {
n[i][j] = arr[index]
index++;
}
}
return n;
Leetcode flagged n[i][j] = arr[index]
as erroneous.
If anyone can shed light on why this error occurred, I would greatly appreciate it. Thank you.