Problem with For Loop
Can someone help me figure out the difference between the function afficher() using for...in vs the for loop? Using for..in the output is correct but with for it's not. am I missing something?
3 Replies
for (index in tab)
a for..in loop will yield the index, wheras a for..of loop will get the value.
The reason the second block is different is because
Means "if the index of the item modulo n is zero", whereas in the first block;
Means "if the item itself modulo n is zero"
To fix the second block, you can use for (const tab of tabs)
instead of for..in
in short;
for..in iterates keys (in an array that's the index)
for..of iterates the values (in an array that's the items)Thank you! that was helpful
np!