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

Can anyone help me with this challenge please.??

Can anyone help me with this challenge please.??

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

for (var i = 100 ; i<=temperatures.length ; i-=1){
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>

1 Answer

Andreas Nyström
Andreas Nyström
8,887 Points

Hi Mike.

The for-loop doesnt look quite right. You have i = 100; which doesnt really make sense. What you want to do is set i = 0; and then loop through all the indexes in the array (i < temperatures.length) - as long as i is smaller than temperatures.length it should keep looping. And end it with going +1 on i. Like this:

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

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

}

You also want to log every index of the array. You use temperatures[i] for this. As i = 0 the first loop it will log out the first item with temperatures[i] as it is the same as temperatures[0]. And as you set i++ it adds one to i after every loop is done. So it is temperatures[1] (the second item] in the next loop. Until there is no more indexes to loop out. It is important that you understand that temperatures.length is actually how many indexes that your array contains. For this one, you have 8 indexes (indexnumbers 0-7). 100 is one index, 90 is one index, 99 is one index and so on.

Does this make sense? If not check out the arrays and loop-videos here again.

Cheers!