This solution is generated by ChatGPT, correctness is not guaranteed. You might also be able to find a solution fromcommunity posts.
AI solution for JavaScript Quiz on BFE.dev
55. sparse array
The code outputs the following:
1
undefined
undefined
2
[2, NaN, 4]
1
undefined
undefined
2
[1, undefined, undefined, 2]
The arr
array has two empty items, which results in gaps or "holes" in the array. When looping through the array with forEach
or for...of
, the empty items are shown as undefined
.
When using map
, the result is an array with the same number of elements as arr
, but with the empty items changed into undefined
, causing the NaN
value when multiplied by 2.
Using the spread operator [...arr]
, the empty items are still present and shown as undefined
.