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

Amad Durrani
Amad Durrani
4,518 Points

Use a for loop to iterate over the values inside the temperatures array. Start from the first element (100) to the last

Can anyone explain why my code isnt working??

Please & thank you.

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

3 Answers

Hey Amad Durrani,

You need to add let keyword before the initialization and add a semi-colon instead of comma.

Here's the final version:

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

To learn more about different loops, you can visit this link: https://www.tutorialrepublic.com/javascript-tutorial/javascript-loops.php

Hope this helps!

Amad Durrani
Amad Durrani
4,518 Points

I also tried

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

Still doesnt work.

Amad Durrani
Amad Durrani
4,518 Points

finally got it

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