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

kevinkrato
kevinkrato
5,452 Points

Requesting Assistance with this Array

I've been plugging through all this stuff all night. I'm worn out. Could someone supply me with the solution to this problem so I can review it tomorrow when go over this material?

thanks, i'll best answer the solution.

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


for (var j = 0; j < temperatures.length; j += 1 ) {
  console.log(j);
}
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
Reza Sorasti
Reza Sorasti
491 Points

0 1 2 3 4 5 6 7

1 Answer

Hey! You're actually pretty close.

The task wants you to display the values in the "temperatures" array. But you've only placed the "j" variable in the console.log, which by itself, will only display it's own value (aka: 0 1 2 3 4 5 6 7, because the "temperatures" length is 8, so it will will show all numbers lower than 8).

When you attach "j" to the "temperatures" array with square brackets "[ ]", the console will display the item which answers to the array's index. 100 is the 0 index, 90 is the 1st index, 99 is the 2nd index, and so on till 10, which is the 7th index of the "temperatures" array. Because array's start the count at 0, not 1, if you want to loop through all of the values, you make "j" < than the array's entire length.

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

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

Good luck with coding!

kevinkrato
kevinkrato
5,452 Points

Hey, thanks again! I dont know if you remembered but the other day I was pretty burned out. Hearing that other people, like yourself, often went back to review material was encouraging, so I've been doing the same and things have been getting easier. Thanks for that.