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 trialBenjamin Bonenfant
4,724 PointsWhat am I missing with this code? Trying to return an array with the last one popped off.
function removeFromBack(arr) { return arr.pop(); } var result = removeFromBack(1, 2, 3); console.log(result);
I know I've missed a step and/or not understanding fully, and grateful for your help.
2 Answers
Steven Parker
231,172 PointsTo create an array literal, you must enclose the values in square brackets:
var result = removeFromBack( [1, 2, 3] );
// ^ ^
Also, the function as shown doesn't return the modified array, what it returns is the item that was popped off.
Benjamin Bonenfant
4,724 PointsThank you, Steven, makes sense. How would I return the array and not the popped item? I think I misunderstood the method as well.
Steven Parker
231,172 Pointsfunction removeFromBack(arr) {
arr.pop(); // pop value off first
return arr; // then return the array
}