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 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,863 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.