FAQ: Arrays: Lesson - Looping Through Arrays

This community-built FAQ covers the “Looping Through Arrays” exercise from the lesson “Arrays: Lesson”.

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

Learn C

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!
You can also find further discussion and get answers to your questions over in #get-help.

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

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

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!

Guys can you help me to resolve the 2nd part of this problem?

Given the integer array arr2 , fill up the array so that each element in the array is four plus the index of the element.


arr[0] = 4 , arr[1] = 5 , arr[2] = 6 , etc.

That phrasing, “four plus the index” is important. How might you translate that to code?

1 Like

So far i get;

#include<stdio.h> int main() { int arr[] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 20, 22, 26, 28, 29}; int arr2[100]; int i = 0, aux = 1; // Code for Checkpoint 1 goes here for (i = 0; i < 20; i++){ printf("%d\n", arr[i]); } // Code for Checkpoint 2 goes here arr2[0] = 4; printf("%d\n", arr2[0]); for (i = 1; i < 100; i++){ arr2[i] = arr2[0] + aux; printf("%d\n", arr2[i]); aux++; } }
1 Like

Hi romanopaulo708735117,

i got to this awmser and i do think that should be right, anyway you should try.


int main() {
  int arr[] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 20, 22, 26, 28, 29};
  int arr2[100];
    for(int j = 0; j < 100; j++)
      arr2[j] = j + 4; 
      printf("%i\n", arr2[j]);

let me know if helped

So I’ve solve this 2nd problem task, and I can access the array elements using printf() but only using the negative indices, if I use positive indices, I keep getting 5, can anyone explain why this is the behavior?
This above is my soultion with the added printf() giving me just rows of 100 '5’s.

You have 100 “5” because you assigned 100 times “4+1”.
You should try to use the current index of the array and add 1 to it.

1 Like

I’m on the first part of this exercise and I cannot figure out how to make it stop looping. Please let me know what I’m doing wrong!

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

// Write your code below

for (let i = 0; i < vacationSpots.length; i++){

console.log('I would love to visit ’ + vacationSpots[i = 0]);


I think you posted in the wrong thread. This is a thread about arrays in C, whereas your question is about arrays in JavaScript. Specifically, you are talking about the exercise: Looping through Arrays
The C and JavaScript exercise both have the same title “Looping through Arrays”, so the misunderstanding is understandable.

The problem is in your console statement:

console.log('I would love to visit ' + vacationSpots[i = 0]);

In the first iteration of the loop, i starts off as 0 because of let i = 0;

When you do vacationsSpots[i = 0], you are assigning 0 to i and then printing the element at index 0 in the vacationSpots array i.e. you are printing the string "I would love to visit Bali"

After the console statement, you finish the iteration. The for loop increments i to 1 and the next iteration of the loop begins. When you get to vacationsSpots[i = 0] in the console statement, you once again reset i to 0. The "I would love to visit Bali" string is printed. The iteration finishes. The loop increments i to 1 and the next iteration begins. You are caught in an endless cycle i.e. an infinite loop. You keep resetting i to ‘0’ in the console statement.

Instead, the correct statement should be:

console.log('I would love to visit ' + vacationSpots[i]);

Now, in the for loop, i will have value of 0 in first iteration, 1 in second iteration and 2 in third iteration. When i becomes equal to vacationSpots.length (which is 3 in this case), then the loop finishes.