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

am lost here..need some assistance.

Use a for or while loop to iterate through the values in the temperatures array from the first item -- 100 -- to the last -- 10. Inside the loop, log the current array value to the console

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

  for (var i= 10, temperatures < 100, i Z++) {
    console.log(temperatures.length[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

Shay Paustovsky
Shay Paustovsky
969 Points

Hi there,

You got a little confused with the idea of iterating through an array using "for loops", but don't get discouraged.

Let me explain:

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

  for (var i= 10, temperatures < 100, i Z++) {
    console.log(temperatures.length[i]);
  }

In this code you already have an array of temperatures ranging from 100-10 and you need to iterate through them. The idea is that you want to iterate through each item in the array and you have set the value to be 10, thus you have less items in the array than the counter variable.

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

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

Now let me explain my code:

I declare a 'for' loop with a counter variable set to the value '0', It's usually a best practice to set it always to 0. Notice that every statement except the increment of i is seperated by a { ; } semi-colon. Than write the condition : I like to say "while x <= / >= than y" run this loop, it would help you in writing the condition better. So "while i (index) is less than the length (num. of items in the array) log to the console the item in the array with the current value of i and then increment it by 1.

Hopefully this helped

Shay