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 trialDurdona Abdusamikovna
1,553 Pointsjust 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
3 Answers
Marcus Parsons
15,719 PointsWell, 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]);
}
Durdona Abdusamikovna
1,553 Pointslol :) you are funny and YES now I learned how to do it with loop :) Thank you again !
Marcus Parsons
15,719 PointsHaha you're of course very welcome! :) Oh, btw, I also added you on Google+ if you don't mind.
Durdona Abdusamikovna
1,553 Pointsnot at all :)
Marcus Parsons
15,719 PointsMarcus Parsons
15,719 PointsNotice 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
1,553 PointsDurdona Abdusamikovna
1,553 PointsHuh ? That is interesting :) Cool, again learned smth new :) and it is possible
Marcus Parsons
15,719 PointsMarcus Parsons
15,719 PointsIs everything clear for you about how I did that?
Durdona Abdusamikovna
1,553 PointsDurdona Abdusamikovna
1,553 Pointswhat if we say
k >= -1
would it help to display the first array ?
Durdona Abdusamikovna
1,553 PointsDurdona Abdusamikovna
1,553 Pointsis it the only way ? I remember in Java we have .reverse() method how about JavaScript ?
Marcus Parsons
15,719 PointsMarcus Parsons
15,719 PointsUnfortunately, 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. :PWhen 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?
Marcus Parsons
15,719 PointsMarcus Parsons
15,719 PointsHahaha yes, I completely forgot about the "reverse()" method! :D
You can just do:
But, at least you have a better understanding of how you can use for loops! :P