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

Durdona Abdusamikovna
Durdona Abdusamikovna
1,553 Points

just an idea is it possible to iterate over arrays in the reverse order ?

Here is my answer to my one of challenges

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

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

in the console I see arrays in the order how they were added and from curiosity thought is it possible to display them in the reverse order ? Thank you beforehand

Marcus Parsons

3 Answers

Well, hello there, stranger! :D

It's very easy :) You just reverse some of the statements inside the for loop and make "k" count backwards:

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

for (var k = temperatures.length; k > -1; k -= 1){
   console.log(temperatures[k]); 
}

Notice one thing, though, that the conditional statement "k > -1" couldn't have been "k > 0" because once k became 0, the for loop would be done, and so that first item in the array wouldn't display.

Durdona Abdusamikovna
Durdona Abdusamikovna
1,553 Points

Huh ? That is interesting :) Cool, again learned smth new :) and it is possible

Is everything clear for you about how I did that?

Durdona Abdusamikovna
Durdona Abdusamikovna
1,553 Points

what if we say

k >= -1 

would it help to display the first array ?

Durdona Abdusamikovna
Durdona Abdusamikovna
1,553 Points

is it the only way ? I remember in Java we have .reverse() method how about JavaScript ?

Unfortunately, no to the first question about "k >= -1", because "k" could then become -1 and the indices of an array start at 0 and go +1 each time. So, they're only defined from 0 to some positive integer. The syntax is k >= -1 as well; you never put the equals sign first. :P

When the for loop makes k = 0, it will execute the console.log at k = 0, and then the for loop will subtract 1 from k and check if "-1 > -1" which is false and will break the loop. Is that a little better?

Hahaha yes, I completely forgot about the "reverse()" method! :D

You can just do:

var temperatures = [100,90,99,80,70,65,30,10];
console.log(temperatures.reverse());

But, at least you have a better understanding of how you can use for loops! :P

Durdona Abdusamikovna
Durdona Abdusamikovna
1,553 Points

lol :) you are funny and YES now I learned how to do it with loop :) Thank you again !

Haha you're of course very welcome! :) Oh, btw, I also added you on Google+ if you don't mind.