Hi guys! I'm learning JS and I haven't understood the if part of this code. Could someone help me, please? | Sololearn: Learn to code for FREE!

0

Hi guys! I'm learning JS and I haven't understood 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);

5/24/2022 11:29:58 AM

Gustavo Rodrigues

2 Answers

New Answer

0

Print i and a[i] to console to see their values

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...