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

Luqman Shah
Luqman Shah
3,016 Points

What does this question mean? What is it trying to ask?

"Use a for or while loop to iterate through the values in the temperatures array from the first item -- 100 -- to the last -- 10."

Ok so what is it asking me to do..?

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

Jeff Wilton
Jeff Wilton
16,646 Points

The next sentence is the important one: "Inside the loop, log the current array value to the console."

You can access the current array value by using the index represented by your "i" variable:

temperatures[i]

Hope that helps, you're pretty close.

Luqman Shah
Luqman Shah
3,016 Points

Ok so I re-attempted the challenge and passed it, but I still don't fully get it. So first, we are testing to see whether the counter is less than the length of the items within the array, it is because by default it holds a value of 0, so it adds one to counter, then the log function logs 1 in the index of the array to the javascript console, the loop repeats, but now since counter has a value of 1, it'll add 2, and the index will change to 2, and it'll log the index of 2 to the console, whicn is 99, so forth and so on. Until the loop ends. Is that right?

Chris Shaw
Chris Shaw
26,676 Points

Hi Luqman Shah,

You have a couple of very minor mistakes in your code which are:

  1. You have a semi-colon after i += 1 which causes a syntax error, the last statement in conditions do not require a semi-colon
  2. You are logging the length of the array for every temperature, instead you need to select the temperature using the i variable declared in the loop
var temperatures = [100,90,99,80,70,65,30,10];
for ( i = 0; i < temperatures.length; i += 1) {
  // i = the current index starting from zero
  console.log(temperatures[i]);
}

Happy coding!

Luqman Shah
Luqman Shah
3,016 Points

Ok so I re-attempted the challenge and passed it, but I still don't fully get it. So first, we are testing to see whether the counter is less than the length of the items within the array, it is because by default it holds a value of 0, so it adds one to counter, then the log function logs 1 in the index of the array to the javascript console, the loop repeats, but now since counter has a value of 1, it'll add 2, and the index will change to 2, and it'll log the index of 2 to the console, whicn is 99, so forth and so on. Until the loop ends. Is that right?