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 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!