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 () 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 () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
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.
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.