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

Lucas Santos
Lucas Santos
19,315 Points

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

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

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]);
}
Lucas Santos
Lucas Santos
19,315 Points

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

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

William Li
William Li
Courses Plus Student 26,868 Points

++ 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.

Trung Nguyen
Trung Nguyen
4,844 Points

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.

James Spence
James Spence
17,563 Points

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

Katiuska Alicea de Leon
Katiuska Alicea de Leon
10,341 Points

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

Samuel Daw
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Samuel Daw
Front End Web Development Techdegree Graduate 16,525 Points

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!

Arturo Espinoza
Arturo Espinoza
9,181 Points

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

Kenny Jeurissen
Kenny Jeurissen
3,894 Points

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

Fernando Guevara
Fernando Guevara
6,172 Points

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.

CJ Williams
CJ Williams
34,372 Points

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

JJ RomMar
JJ RomMar
9,906 Points

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. }

Adarsh Prabhu
Adarsh Prabhu
15,394 Points

You have a typo...

should be

temperatures.length
Katiuska Alicea de Leon
Katiuska Alicea de Leon
10,341 Points

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

Lucas Santos
Lucas Santos
19,315 Points

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.

John Grillo
John Grillo
30,241 Points

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

Riza Barone
Riza Barone
4,973 Points

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

Katiuska Alicea de Leon
Katiuska Alicea de Leon
10,341 Points

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?

Bao Tran
seal-mask
.a{fill-rule:evenodd;}techdegree
Bao Tran
Front End Web Development Techdegree Student 18,959 Points

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]);
}
Koosha Modabbernia
Koosha Modabbernia
7,098 Points

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]); }

sofia Heikkila Svensson
sofia Heikkila Svensson
3,695 Points

Hi,

I´m getting the code wrong :

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

Tyler _
Tyler _
6,651 Points

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]);
}
Abdifatah Mohamed
Abdifatah Mohamed
5,289 Points

How can I reply like this blackboard please?