Why doesn’t this code push the numbers to the same array ?
Each number is printed separately
-
Putting line 3
let arr = [];
inside the if statement gives me the same output
-
And putting line 3 before the for loop gives me this output
Why doesn’t this code push the numbers to the same array ?
Each number is printed separately
Putting line 3 let arr = [];
inside the if statement gives me the same output
And putting line 3 before the for loop gives me this output
I think the array is reinitialized to an empty array every time the loop loops (line 3).
I tried to put it inside the if statement but it gave me the same result, do you know how can i fix this ?
It would really help if you post your actual code instead of pictures.
But yes, if arr is inside of the loop, then you re-initalize it on every loop iteration.
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.