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

Robert Kooyman
Robert Kooyman
4,927 Points

Sorting an array

Why is my code not working? I was not trained to sort an array before but I used the w3schools.com site to find this .sort attribute. Not working unfortunately..

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

for (i=0; i<temperatures.length; i+=1) {
  console.log(temperatures.sort);
}
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

I would not suggest using w3school for ANY help on programming. I would suggest you use MDN as a resource. The issue you are have is the answer is looking for you to iterate to the console the numbers. I would remove the sort method and log the iteration of the variable.

Robert Kooyman
Robert Kooyman
4,927 Points

Thanks. But I am not yet there:

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

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

you are really close, you forgot you need to call the variable inside the console.log and you need to use the square brackets to pass in the iteration variable [i].

console.log(temperatures[i]);