JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 1 Solution

Anna Astle
Anna Astle
11,937 Points

Was there a reason for using a for loop over the other loops for this assignment?

I guess I'm curious about what kinds of settings what prefer one form of loop over another.

1 Answer

Steven Parker
Steven Parker
203,163 Points

It's often possible to implement what you need more than one way. Loop choice can be just based on convenience.

If you know at the start what the initialization conditions should be, what to test to continue, and what to increment for each iteration, the "for" loop is handy because you define them all at once.

A "while" loop might work better if a variety of things might change between iterations, and/or the initialization conditions are present before the loop starts.

And the "do...while" is good for when the loop code needs to be performed before you can test the exit condition.

A "for...in" is great for working with objects where you need the keys, and "for...of" when you only need the values.

And finally, a "forEach" method is handy for arrays or other types of collections that have one.

Does that help?

Anna Astle
Anna Astle
11,937 Points

That is really helpful thank you!