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

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
}