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

For Loop with number value array

hello im on a challeng task to make a for loop to cound down the number item from 100 to 10. This is my code: –––––––––––––––––––– var temperatures = [100,90,99,80,70,65,30,10]; for (var i = 0; i < temperatures.length; i--) { console.log(temperatures); } ––––––––––––––––––– For some reason, it doesn't work in the way I wanted to. Just wondering what have I done wrong. Please feel free to guide me.

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

1 Answer

Yodae Yeshitela
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Yodae Yeshitela
Full Stack JavaScript Techdegree Graduate 19,344 Points

The loop is fine but you are printing out the whole array each time the loop runs. If you index the array using the variable i in the loop it should work. Also the loop should run from 0 to the length of the array so you should increment the variable i

I.e

var temperatures = [100,90,99,80,70,65,30,10];
for (var i = 0; i < temperatures.length; i++) {// i++ instead of i--
console.log(temperatures[i]); //use indexing
}