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

Chris Gutierrez
Chris Gutierrez
6,911 Points

what I m I not understanding?

just using a for loop and believe i am using it right but still getting error saying im not displaying the array of numbers in the correct order. if im starting the i at 0 and increasing by 1 until it is no longer less the the temperatures.length. why is it not displaying correctly?

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

Inside the loop, you'll want to specify which array position to log. For example, temperatures[0] would be the first element which has a value of 100.

Try changing your console.log statement to...

var temperatures = [100,90,99,80,70,65,30,10];
for (var i = 0; i < temperatures.length; i += 1){
    console.log(temperatures[i]); // notice the i in brackets
}

...and see if that does it for ya. :)

Chris Gutierrez
Chris Gutierrez
6,911 Points

thanks. that did fix the issue. i can't believe i forgot that.

Michal Weizman
Michal Weizman
14,050 Points

Hi Chris, The assignment is to iterate the array items one by one. Look inside your console.log() command. You are calling the entire array. Try adding an [i] in there.

I hope this helped.

Chris Gutierrez
Chris Gutierrez
6,911 Points

Hello Michal. thank you for your help. May need help further down the line. thanks.