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

This is challenge is slightly stumping me!

Hi there, So, you can see this the layout of this challenge, and I'm a bit stuck. I'm pretty sure I butchered the for loop, but I'm not sure how to meet the requirements that I'm supposed to use in the for loop. Any suggestions?

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

for ( i = 0; i < 100; i += ) {
  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

Michal Weizman
Michal Weizman
14,050 Points

Hi Gabriel, You should use the variable i inside the console.log in order to print out all of the items in the array. Temperatures is the name of the array, but you need to write temperatures[i] for each array item. Also, you do not want to iterate through the array 100 times - but only through the length of the array which is equal to: temperatures.length. Like so:

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

Ahhh, that's what it is! I was about to do that right off the bat, but I decided against it. I understand now. Thanks for your help!

Casey Ydenberg
Casey Ydenberg
15,622 Points

Hi Gabriel,

As a hint, you'll want your i to refer to the index/position within the array, rather than the actual value. So you are correct to start at i = 0 (though you should use var i = 0) ... but the condition for ending the loop should take into account that i will refer to the index and not the value. (ie 100 is way too high).

Does that get you any closer?

Yes, indeed! Thank you both for the helpful hints and advice!