Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Iterating through an Array

use a for loop or a while loop and make array

I am not sure what there asking here so its hard for me to answer the question

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.

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

script.js
var temperatures = [100,90,99,80,70,65,30,10];
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

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.

    // using a for loop, we are going to iterate--step through the array--values of an array called temperatures.
    for (let i=0; i < temperatures.length; i++) {  // declare a variable i set to zero, the first indexed element in array
        // while i--the index of the element in the array--is less than temperatures.length--8--print i to the console.
        console.log(i);
    }

The logic is the same with a while loop and is set up slightly different. you would want to initialize a var to zero and loop while (i < temperatures.length) and increment i each time you print to the console. I've had a lot of practice using these two types of loops and this is how I was able to dissect this. Keep up the practice and it will make more and more sense!

Cheers!