FAQ: Loops - Looping through Arrays

#1

This community-built FAQ covers the “Looping through Arrays” exercise from the lesson “Loops”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise Looping through Arrays

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

#2

const animals = [‘Grizzly Bear’, ‘Sloth’, ‘Sea Lion’];
for (let i = 0; i < animals.length; i++) {
console.log(animals[i]);
}

I’m having a conceptual problem with how this can be the stopping condition:
i < animals.length

So, assuming:
[‘index 0’, ‘index 1’, ‘index 2’]
[‘Grizzly Bear’, ‘Sloth’, ‘Sea Lion’]
then it seems to me that the code block should stop when i (index value) is less than animals.length (3), which always has to be true, because beyond i=2, there are no values for i.

What am I seeing incorrectly?

#3

When it fails, the loop stops.

stopping condition is not really the term to describe the conditional expression here, but so be it if that is how the teacher wishes to describe it being that it is a conditional that effects termination of the loop. Perhaps stopping conditional would be more apt so we don’t read in that it is the condition that will stop the loop. Quite the opposite, it is the condition that allows the loop to continue.

As it is written in the code, we could see it as the looping condition which is dependent upon ‘true’ to keep going.

#4

MTF’s thought to see it as a
“continuing condition”, rather than a “stopping condition”, helped me out a little, and I think I get it now. i is not equivalent to a value in the code block, rather to an index position. So it’s better to think of i as a generated number (starting off at ‘0’, the iterator) which will serve as index positions in the code block.

If i=0, the operation will perform, since it’s less than 3. (and then the the index position of ‘0’ will be put into the code block, which will then log to the console whatever is in that index position in the array: i.e., ‘Grizzly Bear’.)
If i=1, the operation will perform, since it’s less than 3. (and then the the index position of '1" will be put into the code blcok, and the console will log whatever is in that index position in the array: ‘Sloth’.)
If i=2, the operation will perform, since it’s less than 3. (and then the the index position of ‘2’ will be put into the code blcok, which will then pointt to what’s in that index position in the array: ‘Sea Lion’.)
if i=3, the length of the array,the code block will not run, and the loop is terminated.

Ugh, not sure if that helped. May have caused more confusion! But I think the word “value” points to where your issue is.

Best,
Nick

1 Like
#5

Thanks for the explanations and step-by-step walk-through! I understand it much better now. My main misunderstanding I think was assuming the stopping condition (more aptly referred to as the looping/continuing condition) stopped the block from running if it outputted TRUE but, if I understand correctly, an output of TRUE continues the block, whereas FALSE stops it from running.