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

Conner Williams
PLUS
Conner Williams
Courses Plus Student 3,305 Points

how do you complete this exercise?

having trouble getting this one right.

script.js
var temperatures = [100,90,99,80,70,65,30,10];
for(i=100; i < temperatures.length; i-=1) {
  console.log(temperatures[i]);
}
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

Remember that you should use the index (location) in the array instead of the array value itself. var temperatures = [100,90,99,80,70,65,30,10]; temperatures[0] --> should give you the value of the first item in the array --> 100 in this case temperatures[100] --> doesn't exist since there are only 10 items in the array

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

We defined i = 0 since we want to start from the beginning i++ is the same as : i + 1

This means for each item in the array, add 1 to i which in return console logs the value of i as index of the array.

Hey conner try this

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

Happy coding

Paul

You start at 0, which is the first item in the array (javascript is zero based), not the numerical value of 100, then loop over them using i += 1

Hope i explained it ok