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

Whats wrong with my code?

I'm getting a parse error

script.js
var temperatures = [100,90,99,80,70,65,30,10];
var tlen = temperatures.length();
var i;

for (i = 0, i > tlen, i++) {
      console.log(temperature[i]);
}
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

Hey

You need to remove the parenthesis from the second line, that denotes a function. In fact, you can remove the second and third lines so the code is a little cleaner. Also, use semi colons inside the for loop parenthesis.

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

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

So the var i is declared, and the length property on the temperatures array are passed to the for loop. While i is LESS than the length of the temperatures array it keeps iterating over until it reaches the end.

Hope that helps, happy coding

Paul

Magnus Hållberg
Magnus Hållberg
17,232 Points

First of you separate the statements in the for loop with semicolon, also you have created a variable “i” that the for loop takes care of it self so that you can delete. You don’t have to create a variable for the array length, you could just call temperatures.length inside the for loop declaration but I don’t think this is giving you an error. Finally, this loop will go on as long as “i“ is greater then .length witch it’s not since it’s set to 0, it should be as long as it’s less then .length. Hope this helps.