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

amit dilawri
amit dilawri
1,244 Points

Challenge Task 1 of 1 - Using "for" or "while" loop in an array.

Use a for or while loop to iterate through the values in the temperatures array from the first item -- 100 -- to the last -- 10. Inside the loop, log the current array value to the console.

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

I used the compare function from Mozilla Developer Network. Solution works on console however this answer was not correct. Any help solving this challenge is much appreciated.

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

for (var i = 0; i < temperatures.length; i += 1){
    temperatures.sort(function(a,b) {
        return b - a;
    })
    console.log(temperatures[i] + "<br />");
}
script.js
var temperatures = [100,90,99,80,70,65,30,10];
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

5 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! The biggest problem here, is that you've gone WAY above and beyond the call of duty here. You've sorted the array. They didn't ask for that so when you log out the numbers they're in the incorrect order. They also do not need you to log out a <br />. Take a look:

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

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

Our temperatures array is given to us by Treehouse. They want us to go through the array and log out the numbers one by one without sorting them first. So we set up our for loop and go through each element and log it out to the console. Hope this helps! :sparkles:

I used: +=

This works as well, why?

for ( var i = 0; i < temperatures.length; i += 1 ) {
  console.log(temperatures[i]);
}
Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

David Ryan Actually, the code you wrote there will produce a syntax error, but I believe you meant i += 1. The code i++ does exactly the same thing as i += 1. It takes the value of i and increments it by 1. The ++ operator is used in multiple languages besides JavaScript including(but not limited to), C/C++/C#, earlier versions of Swift, and Java.

The benefit to using the += over the ++ is that you can specify how much you want to increment by each time. The ++ always increments by one. But you could do something like i += 5, and every time through the loop, that i variable would increase by 5.

Hope this clarifies things! :sparkles:

Sun-Li Beatteay
Sun-Li Beatteay
10,606 Points

I think you're just overthinking it a bit. I think they just want a simple for while loop with the console.log method

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

for (var i = 0; i < temperatures.length; i += 1){
    console.log(temperatures[i]);
}
amit dilawri
amit dilawri
1,244 Points

Thanks Jennifer and Sun-Li for your help. It works now. I did spend hours figuring out how to sort the list in descending order.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

But as a bonus, you now know how to sort arrays! :smiley: And likely as not, you'll need that skill at some point :sparkles:

Chris Drew
Chris Drew
5,919 Points

First thing is to set up your for loop. how many items are in the array? You could just count the numbers and say something like i < 7; but that code would break if we added any more temperatures.

Instead use the array's .length property. and use i ++ to iterate to the next item in the array each time the code runs.

Since we need to log an array, our console.log statement needs to contain braces.

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