DEV Community

Understanding Javascript Array Series V - Array Loops & Iteration Part II

Nedy Udombat on October 03, 2019

In the previous article, I talked about iterating over arrays using For ..., For ... of & For ... in loops. You can check it out below: ...
Collapse
 
rolandcsibrei profile image
Roland Csibrei • Edited

How can you use a variable name "size" for index access? This is a very bad idea. Sure, this is only a short example, however you are seeding the devil into the newcomers minds. Variable naming is crucial. Another thing is that the index variable should be initialized to size-1 when entering the while loop and should be decremented at the end for better readibility. Just my three-- cents.

Collapse
 
nedyudombat profile image
Nedy Udombat

Hi @rolandcsibrei , thank you for the correction, I appreciate it. But however what I will not appreciate is this statement however you are seeding the devil into the newcomers minds. If you want to give someone feedback make it ASK(Actionable, Specific and Kind). Thank you.

Collapse
 
rolandcsibrei profile image
Roland Csibrei

Sorry, I didn't want to be offending. I apologize.

 
nedyudombat profile image
Nedy Udombat

This has nothing to do with prefixing and suffixing the ++ an -- operators. I am saying that, when you initialize the index to 0, when the condition (index++ < players.length-1) is executed the value of index becomes 1 and the first element of the array is skipped.

Collapse
 
nedyudombat profile image
Nedy Udombat • Edited

@dovca , I see your point on calling it statements instead of functions, I will correct it to that effect.

The reason for using the .reverse() method was explained in the article, just above that section. Seeing this other approach I will, add that to the article and give due credit.

Setting the index to 1 will not work for the while loop cause at first iteration the value of index will become 1 because of the increment in the condition. However initializing index to -1 will work.

Thank you.

Collapse
 
mchardex profile image
Adebisi Oluwabukunmi

Awesome refresher. Interesting examples

Collapse
 
nedyudombat profile image
Nedy Udombat • Edited

Thank you @mchardex , I'm glad the article was of help. 👋

Collapse
 
faruq2 profile image
Faruq

Very insightful
Nice one!

Collapse
 
nedyudombat profile image
Nedy Udombat

Thank you @faruq2 . I am glad you found this useful.

Collapse
 
profjigsaw profile image
Nweze Victor Chinweudo

Awesome stuff! Keep them coming.

 
nedyudombat profile image
Nedy Udombat

Thanks for your timely corrections though, I appreciate. @dovca

Collapse
 
rolandcsibrei profile image
Roland Csibrei

It was an intention to write it that way to reflect the sample code ;-)