0

Hi guys! I'm learning JS and I didn't understand the if part of this code. Could someone help me, please?

var a = [2, 1, 2, 0, 4] var b = [] for(i = 0; i <= a.length; i++) { if (i == a[i]) { b.push(++i); } } console.log(b.length);

24th May 2022, 11:29 AM
Gustavo Rodrigues
Gustavo Rodrigues - avatar
2 Answers
0
Print i and a[i] to console to see their values
24th May 2022, 11:40 AM
Lisa
Lisa - avatar
0
If value of array at index i is equal to the index i, then add ++i to b list. At indexes 1,2,4 the values are 1,2,4 so adding 2,3,5 to b list. Hope it helps...
24th May 2022, 11:43 AM
Jayakrishna šŸ‡®šŸ‡³