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

Srdjan Cestic
Srdjan Cestic
6,855 Points

Iteration

var temperatures = [100,90,99,80,70,65,30,10]; for ( var i = 100; i => 10; i = i + 1 ) { console.log(temperatures); }

1 Answer

Gavin Ralston
Gavin Ralston
28,770 Points
var temperatures = [100,90,99,80,70,65,30,10]; 
for ( var i = 100; i => 10; i = i + 1 ) { 
     console.log(temperatures); 
}

If you're having trouble making this work, remember that the variable i is not the same thing as the value in temperatures. So checking until "i => 10" won't really do much except run the loop once.

If the idea is to print each item in the array, break the problem of setting up the three parts of the for loop like this:

  • Set up a counter that starts at 0, call it "index" to keep it straight in your head
  • check to see if the index is less than the length of the array (so it'll run til we're out of stuff to read)
  • increment the index by 1

Then in the loop block, be sure to do this:

  • console.log(list_of_things[index]) for each one. Remember to use the index to get the item out to print!
Srdjan Cestic
Srdjan Cestic
6,855 Points

It work, it seems that this random numbers just confuse me, thanks a lot Gavin.

Gavin Ralston
Gavin Ralston
28,770 Points

Glad I could help! Good luck. :)