Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Iterating through an Array

brayant benitez
brayant benitez
7,885 Points

having some problems understanding looping arrays.

okay so i am working on the excercise too loop an array, now i cant seem to understand why i must change temperature to say "i" i dont really seem to understand where that is coming from, i assume since the var is temperature, i should use temperature in the loop as the variable.

script.js
var temperatures = [100,90,99,80,70,65,30,10];
for (temperatures == 10; temperatures <=100;temperatures++){
  console.log temperatures
};
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

1 Answer

Thomas Fildes
Thomas Fildes
22,687 Points

Hi Brayant,

"i" is a temporary variable which is used for indexing loop iteration. You use "i" as sort of like a counter in which you will count through the indexes in the temperatures array. Here is the code I used to pass this challenge below:

var temperatures = [100,90,99,80,70,65,30,10];

for (i = 0; i <= temperatures.length; i++) {
  console.log(temperatures[i]);
}

As shown above I have used a for loop for this example but you can alternatively use a while loop. The loop starts and runs the console log at i = 0 and ends when the condition "i <= length of array" is not fulfilled . Since the index starts at 0 we will log all the items in the array because between indexes 0 and 7 there are 8 items.

I hope this helps you briefly understand iterating through arrays but if not just practice indexes with loops. I was in the same position as you once and the key thing is to never give up. Happy coding!!!