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 Foundations Arrays Methods: Part 1

Yusuf Salaam
Yusuf Salaam
1,748 Points

Im getting stuck on step 3 of array challenge. Can someone please show me how to do it?

var spareWords = ["The","chimney","sweep's","dog"];
console.log(spareWords.toString())

//Shift and unshift method
var firstWord = spareWords.shift();
spareWords.unshift("firstWord");
console.log(spareWords.toString())

//Push Pop method
console.log(spareWords.toString())

spareWords.push("lastWord");
console.log(spareWords.toString())

var lastWord = spareWords.pop();
console.log(spareWords.toString())
var lastWord = spareWords.pop();
      var saying = ["quick", "brown", "fox", "jumps", "over", "the", "lazy"];

This is what I have so far!

2 Answers

I'll give you a tip ...

var spareWords = ["The","chimney","sweep's","dog"];
var saying = ["quick", "brown", "fox", "jumps", "over", "the", "lazy"];
/* var firstWord = spareWords.do-something-here */
/* var lastWord = spareWords.do-something-here */
/* saying.do-something-here */
/* saying.do-something-here */
Peter Szerzo
Peter Szerzo
22,661 Points

sorry, Ryan, for giving it away :)

Peter Szerzo
Peter Szerzo
22,661 Points

Hey Yusuf,

Try this:

var firstWord = spareWords.shift();

var lastWord = spareWords.pop();

saying.unshift(firstWord);

saying.push(lastWord);

The array commands are as follows: push - add element to end of the array pop - take element off the end of the array (and return it) unshift - add element to the beginning of the array shift - take element off the beginning of the array (and return it)

This article might help: http://www.bennadel.com/blog/1796-javascript-array-methods-unshift-shift-push-and-pop.htm

Also, fun conversation on why these methods are named as they are: http://www.perlmonks.org/?node_id=613124

Cheers, Peter