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 trialJasmany Lewis
1,474 PointsCan't seem to iterate through this array.
Can someone please tell me what i am doing wrong?
var temperatures = [100,90,99,80,70,65,30,10];
for(var i = 0; i > temperatures.length; i -=10){
console.log(i) ;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
3 Answers
Ryan Waite
7,678 PointsThe index value is used to iterate through the array. In your array,
- temperatures = [100,90,99,80,70,65,30,10]
- 100 has an index of 0
- 90 has an index of 1 and so on.
The for loop you created is taking 10 away from the index value so you will never get to the next item in the array. To fix try i += 1 or i ++.
for ( var i = 0; i < temperatures.length; i ++) {
console.log(temperatures[i]);
};
Hope this helps
Adam Beer
11,314 PointsThe solution is a bit different. First, now the i greater than the temperatures.length inside the for() loop. This is false, because i less than the temperatures.length. So you can modify your operator to the right operator. Second, i -= 10. This isn't true, because 100 - 10 equal to 90 but 90 - 10 not equal to 99. So this is the second bug. Just go through it smoothly, do not complicate. So you can modify this section to i++ or i += 1 both are the same. Finally, inside your console.log(). You can use the variable name outside your loop ,and after put your new variable name. Like this, console.log(your old variable name[your new variable name]). Hope this help!
Jasmany Lewis
1,474 PointsThank you Adam! This was really helpful.
Jasmany Lewis
1,474 PointsRyan Waite Thanks a lot bud! Your help is appreciated. Makes sense!