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
melissakeith
3,766 PointsArray reversal, need help!
I've tried these functions but when I try to cosole.log() they return a blank array.
function reverseArray(array) {
var arr = [];
for (var i = array.length - 1; i > 0; i--) {
}
return arr;
}
console.log(reverseArray([1,2,3,4,5]));
function reverseArray(array) {
var arr = [];
for (var i = 0; i < array.length; i > 0; i++) {
arr.unshift(array[i]);
}
return arr;
}
console.log(reverseArray([1,2,3,4,5]));
3 Answers
Ira Norba
1,155 Pointsfunction reverseArray(array) { var arr = []; for (var i = 0; i < array.length; i++) { arr.unshift(array[i]); } return arr; } console.log(reverseArray([1,2,3,4,5]));
Ira Norba
1,155 Pointsdelete conditional "i > 0;"
melissakeith
3,766 PointsI tried it that way before and got the same thing, a blank array. So I tried adding and changing things around.
melissakeith
3,766 PointsNevermind that did work, must not have been paying attention to all the details before. Could you also look at the 2nd function and give suggestions to make that one work as well? I know there is two ways to do this at least, and I want to understand both.