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

Jasmany Lewis
Jasmany Lewis
1,474 Points

Can't seem to iterate through this array.

Can someone please tell me what i am doing wrong?

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

for(var i = 0; i > temperatures.length; i -=10){
 console.log(i) ;
}
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

3 Answers

The index value is used to iterate through the array. In your array,

  • temperatures = [100,90,99,80,70,65,30,10]
  • 100 has an index of 0
  • 90 has an index of 1 and so on.

The for loop you created is taking 10 away from the index value so you will never get to the next item in the array. To fix try i += 1 or i ++.

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

Hope this helps

Adam Beer
Adam Beer
11,314 Points

The solution is a bit different. First, now the i greater than the temperatures.length inside the for() loop. This is false, because i less than the temperatures.length. So you can modify your operator to the right operator. Second, i -= 10. This isn't true, because 100 - 10 equal to 90 but 90 - 10 not equal to 99. So this is the second bug. Just go through it smoothly, do not complicate. So you can modify this section to i++ or i += 1 both are the same. Finally, inside your console.log(). You can use the variable name outside your loop ,and after put your new variable name. Like this, console.log(your old variable name[your new variable name]). Hope this help!

Jasmany Lewis
Jasmany Lewis
1,474 Points

Thank you Adam! This was really helpful.

Jasmany Lewis
Jasmany Lewis
1,474 Points

Ryan Waite Thanks a lot bud! Your help is appreciated. Makes sense!