FAQ: Loops - Looping through Arrays

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!

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 Likes

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.

10 Likes

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

26 Likes

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.

3 Likes

Would this loop not work if the array looked like this

vacationSpots[‘a’, ‘b’, ‘c’];
?

also where it says ’ i < vacationSpots.length’ which vacationSpots.length does it check itself with ?

edit: I made a mistake I thought vacationSpots.length checked the length of the words in the array rather than the amount of values in that array

2 Likes

The length is the count of elements in the array, just for clarification.

1 Like

I used the following code originally and got the right resut, but was marked wrong.

const vacationSpots = [‘Bali’, ‘Paris’, ‘Tulum’];

// Write your code below
for( i = 0; i < vacationSpots.length; i++){
  console.log("I would love to visit " + vacationSpots[i]);
}

Afterwards I added the “let”, and my code didn’t change, but I was allowed to pass. What was the difference between my code with and without the let

2 Likes

let gives variables block scope which keeps them from leaking into their parent scope, or worse into global scope. It’s actually a very important concept in JS.

7 Likes

when i did

for(let  i = 0;  i = vacationSpots.length;  i++) {
   console.log(i);
}

I got a really long list of the vacation spots. but according to the calculations above, shouldn’t i have gotten only one extra line of vacationSpots?

1 Like

That should be a relation check, not an assignment.

i < vacationSpots.length;
1 Like

if there are 3 vacationSpots, and with index value its 2 ( because you count from 0), then .length should give me ‘one’ more. no? (till 3)

1 Like

ok, i get it, this would be true if it would have been a stop condition. it would go on, till it saw it did it already 3 times. but since this is not a ‘stop condition’ , but rather a looping condition, it will go on and on, because it will always answer true

2 Likes

Can’t I use var instead of let?

for (var i = 0; i < vacationSpots.length; i++) {
  console.log(`I would love to visit ${vacationSpots[i]}`);
}

It just keep asking me to use let! So what is the problem of using var?

1 Like

It’s not a problem, per se, only that it is a legacy keyword that has gone out of fashion in ES6. let is intended to overcome a weakness that var presented by introducing true block scope. The author is likely hoping to help learners get away from using it. let is the more preferred keyword, these days and moving forward.

1 Like

I keep getting an error when trying to run this exercise. The only way I could proceed was by getting the solution, which to me looks exactly like my code. Is it a bug?

for (let i = 0; i < vacationSpots.length; i++) {
  console.log('I would love to visit ' + vacationSpots[i]);
}

Even when getting the solution, copying the result, resetting the exercise and trying to run the solution, I get an error.

2 Likes

Great answer Nick! Good job sir! That was very helpful and through answer.

1 Like

for (let i = 0; i < vacationSpots.length; i++){
console.log(I would love to visit ${vacationSpots[i]} )
}

this said i was wrong! pls tell me what im doing wrong

Hello, @danielthecode.

Welcome to the forums.

I don’t see anything wrong with your code. Can you copy & paste the error message you received?

// const placesToGo = [‘korea’,‘africa’,‘japan’]

// for(let i=0 ; i<placesToGo.length ; i++){
// console.log(These are the places I want to go ${placesToGo[i]})
// }

const placesToGo = [‘korea’,‘africa’,‘japan’]
//reverse
for(let i = placesToGo.length;i>-1 ; i–){
console.log( I want to go to ${placesToGo[i]})
}

I have understood the for loop to go over arrays but if I wanted to reverese it I have been trying many ways but I keep getting an undefined.

I want to go to undefined
I want to go to japan
I want to go to africa
I want to go to korea

Where is this undefined coming out from can I have some explaination…

Thank you