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!

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

Mitchell Kienhuis
Mitchell Kienhuis
2,507 Points

why won't this work, I have to display the current value of the tempratures variable to the console log?


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

for(var i = 0; i < temperatures.length; i+=1){
<!DOCTYPE html>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
<script src="script.js"></script>

2 Answers

Tim Knight
Tim Knight
28,888 Points


You're really close here. You won't be needing that shift method on the array, you just need to use your loops index and then pass the index using bracket notation.

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

for(var i = 0; i < temperatures.length; i+=1){

Hi Mitchell,

If you want to display all the temperatures in the array with your for-loop you will need to change the shift method to [i] like: console.log(temperatures[i]);

This is to iterate through the array successfully.

...With that being said, the shift method is to get rid of the first item in the array so I'm not sure what you would like to do.

Let me know if this helps.