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

Cant seem to figure out what is wrong trying to convert this into a for loop

Trying to convert this array into a for loop.

script.js
var temperatures = [100,90,99,80,70,65,30,10];
for (var i= ['']; i == [''] ) {
   i = temperatures;
}
 console.log(temperatures);
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

Your for loop is incorrect. First, you wrote i=[''], which is incorrect. Usually you want i to equal zero, since an array's first object equals zero. Second, i shouldn't be equal to something, otherwise the loop could only run once or forever. In this case, it should be less than the amount of values in the temperatures array, or the length. use the .length property so you can change the array, without having to worry about the loop. You also need to increase I, so it reaches the limit, and the loop stops, so you need to add i+=1. To write it somewhere, for example the console, you can do this:

for (var i = 0; i < temperatures.length; i+=1) {
console.log(temperatures[i];
}
Jeff Wilton
Jeff Wilton
16,646 Points

I would encourage you to go back to the previous video and watch it again, but here are some hints.

The var used inside a for loop should be an integer so you can increment it up to a certain limit (which you need to set).

All in all, it would look something like this:

var students = ['Sacha', 'Lynn', 'Jennifer', 'Paul'];
for (var i = 0; i < students.length; i += 1) {   //starting at index 0, count up by ones up to the length of the students array
   console.log(students[i]);   //print the student at the specified index
}