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 Arrays Loop Through Arrays Loop Through an Array

Angelica Islas
Angelica Islas
4,082 Points

Log the current array value. Not sure how to solve this one.

script.js
const temperatures = [100, 90, 99, 80, 70, 65, 30, 10];
for (i=100; i< 10; i+=) {
  console.log (myArray [i]);
}

1 Answer

Okay, so what they are asking you to do is loop through the "temperatures" array and print each value to the console.

First we need to declare the variable "i" in our for loop,

Then, remember, arrays start at index '0'

Then, you can either count the number of items in the array manually, or you can use the in built array method "array.length" to get the total number of items in the array.

Then we want to increase the value of "i" each time we "loop" through which can be written as "i++"

Last thing we want to do is print the value of the "temperatures" array at index "i" which can be written as "temperatures[i]

So, if we want to loop through from the start of the array at index 0 to the end of the array("temperatures.length"),

We could write that as:

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

hope that helps!