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

?

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

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

2 Answers

Tim Knight
Tim Knight
28,888 Points

Mitchell,

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){
    console.log(temperatures[i]);
}

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.