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

Christopher Smith
Christopher Smith
1,963 Points

I don't know or understand what I am doing wrong.

It is my understanding that the value for the first item within an array is 0. The challenge asks me to iterate through the loop starting at 100 then finish 10. I am lost and Im beginning to think I missed something.

Please help.

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

for (var i = 0; i < temperatures.length; i += 1) {
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>

2 Answers

Christopher,

You missed one little thing. Within the console.log() function you need to remember to include the index for the temperatures array, temperatures[i]. Without it there's no way to know what item in the array needs to be logged.

Christopher Smith
Christopher Smith
1,963 Points

Awesome, thank you kindly Kyle!!!

Your for loop holds a variable. The variable in your case is "i" and as such, this variable changes every time the loop is completed. This variable can be used inside the functional part of a loop as a counter for different things. In your case, the variable would go through your temperatures array.

Every array property has a value, this value starts at zero and increases by one with every item inside the array, an example of your array would be;

var temperatures = [100,90,99,80,70,65,30,10]
// temperatures[0] (this is 100) 
// temperatures[1] (this is 90)
// temperatures[2] (this is 99) 
// and so on..

Your for loop holds the variable i, which starts at zero, checks to see if the "i" is less than the length of your temperatures array (i < temperatures.length) and then adds 1 to the "i" variable (i += 1)

If you change your console.log(temperatures) to console.log(temperatures[i]) you'll see this change in action with your for loop.

Christopher Smith
Christopher Smith
1,963 Points

Thanks Edin!!! Much appreciated!