Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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. :)