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

Stuck on this javascript loop

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];
temperatures.sort(function(a, b){return a-b});
for( i = 0; i > temperatures.length; i++){
console.log( temperatures[i] );
}

tried my best and could of sworn I got this correct because iv done this before.

index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

16 Answers

You don't need to sort the items in the Array, so delete this line temperatures.sort(function(a, b){return a-b});

just use the for loop to log out each item

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

Yeah I was just missing the var before the i. Thanks

What is the significance of the "++" behind the 'i' in that code?

++ in JavaScript, as well as many other programming languages, is an increment operator, i++ means increment i by 1, it's a shorthand for i = i + 1.

Hello, I did console.log([i]); and its wrong. Could you tell me why you need temperatures[i]?

Thanks, this was the best answer I found.

Check the condition in your for loop ' i > temperatures.length '

You need "temperatures" in console.log(temperatures[i]); because it is the name of the array.

Try this:

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

too bad the they don't actually teach you how to do the challenge in the video

facts!

Is +=1 same as i++ ? I don't recall this being in any of the lessons.

Yes, it is. It's not in any of the lessons, I found it out while doing CodeCademy's Javascript course.

yes

yes the same happend to me with (++) pass but with (+=) gives me an error, i don't understand this if both are supposed to be the same

Yes, it is.

won't work with a semi-colon after the for loop.

var temperatures = [100,90,99,80,70,65,30,10]; for (i = 0; i < temperatures[7]; i+= 1) { // Can use: for (i = 0; i < temperatures.lenght; i++) console.log(temperatures[i]); // but it doesn't run. }

You have a typo...

should be

temperatures.length

I had a typo on length too, that's why it wasn't running! Lol

hmm I guess I was rite I just passed by adding var before the i in my for loop. Don't get why you need the var tho, iv been writing my javascript for loops for a long time without the var. Believe javascript already knows that the first thing in the for has to be a variable.

that's the answer; is should be i < temperatures.length also, a var i = 0 at the beginning wouldn't hurt.

????

what does temperature.length mean in this for loop? I am having a hard time understanding it...

As I understand it temperature.length takes the place of the number you would have put there as the number of times the loop needs to run. Since items can be added or subtracted from an array at any time, for any reason, without you being there all the time to make sure the loop runs for the number of items in the array, you place the .length property, which will ALWAYS have the number of items in the array, hence the number of times the loop has to run.

This may be a stupid question but does the [i] variable only count for that loop?

Ryan Roybal , Not sure if this answer your question but may also help others. It is not really a count.

i itself is just a makeup shorthand variable, like i or j from previous lessons. What I've found very interesting is when in JavaScript, if we set the " var i = 0 " , now that happen to match up exactly as the array index value.

So when we do " console.log(temperature[i]) " , it print out all the value from the array , in this case 0,1,2,3,4,5,6,7 because of the for loop " for (var i = 0; i < temperatures.length; i += 1) "

Short answer [i] here in this problem is the stored values from the for loop.

Hope this help.

What did I do wrong

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

I think there shouldn't be ";" after the i += 1

Wow. Again, the videos, not the teachers (because you call teachers when you get answers to your questions/inquiries and I'm stuck in Laravel, I never received the support), never teach too much, the challenges are to make you learn how bad trained are you in this, anyway, I got the correct answer to this challenge and the response below

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

Hi,

I´m getting the code wrong :

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

I believe this will work too:

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

How can I reply like this blackboard please?