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

Why is my for loop not working?

Not sure why it isn't working

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

for (i=0; i <11; 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>

3 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! I'm going to give some hints here. Hopefully, you can get the answer from these as I think it will be a better learning experience overall.

  • Your code is currently logging out the numbers 0 to 10.
  • Your code should be logging out the element in that position of the array where i is less than the length of the array
  • You will need to access the array set up by the challenge like so temperatures[i]

Good luck, but let me know if you're still stuck! :sparkles:

Hey Jennifer, your profile is impressive. May I ask how many hours a day do you come into Treehouse?

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Wilfredo Casas I used to come here no less than 8 hours a day. It's been less recently and some days not at all. I'm currently working on some projects :smiley:

I take my hat off to you madam.

Add a counter variable to do your counting. x = count.length

It won't break when you add more or less

Luiz Felipe Soares
Luiz Felipe Soares
13,840 Points

Just to be safe in the future, you can try to set i < temperature.length , so you work with variables and minimise human error.

Always remember that "i" make reference to the index of your array in your case.

cheers