I change the code to console.log("result is"+onesArray);
the console.log is:
So, the array is really empty.
Maybe when console.log is empty, the console will show the executed array as if there is a code console.log(a(3))
Anyway, thank you for your replay!
One interesting thing about this is try and remove the console.log and run the code in Dev Tools. what you see is (3) [1, 1, 1] which is from the return oneArray.
So the console log runs one time before the for loop and accounts for why, Jessica, you were seeing [ ] on one line then (3) [1, 1, 1] on the second line.
I was also confused why the console log would run twice in this function (and it doesn’t - it only runs once). We would have to run it after the for loop to see (3) [1, 1, 1] but really because the return also shows the same, it’s really (3) [1, 1, 1] twice.
You should get two outputs. One will be an empty array that you’re printing out in the 3rd line console.log(onesArray). It’s empty simply because you’re printing it out before looping through and adding the numbers (1 in this case). And the 2nd output will be the one because of the return statement that’s returning the modified array (by the for loop in this case) which will be an array consisting of 3 1’s like this one: [1, 1, 1]but it won’t print out the output because you haven’t put the function call a(3) inside a console.log(), so replace it by console.log(a(3)) and the problem should be fixed!