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 trialMark Bradshaw
Courses Plus Student 7,658 PointsReally lost on this question. doesn't make sense based on the lesson.
On line 18, use a method on the 'spareWords' to set 'firstWord' to be first word of the 'spareWords' array whilst simultaneously removing it from the beginning of the array. 16 var spareWords = ["The","chimney","sweep's","dog"]; 17 var saying = ["quick", "brown", "fox", "jumps", "over", "the", "lazy"]; 18 var firstWord = spareWords; 19 var lastWord = spareWords;
I know how to add and remove an element using unshift and shift, but nothing that i've done has worked here. The simultaneously part is also very confusing.
1 Answer
Erwin Meesters
15,088 PointsYou are really close: You can use shift() to take the first element of an array and remove it. To assign it to the variable firstWord try this:
var firstWord = spareWords.shift();
notf0und
11,940 Pointsnotf0und
11,940 PointsMark, while Erwin has provided a good answer, I do suggest reading up on the .shift() method to fully understand how and why it works: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift.
To quote from that article: "The shift method removes the element at the zeroeth index and shifts the values at consecutive indexes down, then returns the removed value. If the length property is 0, undefined is returned." So while it removes the value at index 0, it also returns that value, allowing you to set it to the firstWord variable.
I hope that helps a bit in understanding .shift().
Mark Bradshaw
Courses Plus Student 7,658 PointsMark Bradshaw
Courses Plus Student 7,658 PointsThanks Erwin. Cheers. I have more reviewing to do in this area. Thanks for posting the article Ryan.
Mark Bradshaw
Courses Plus Student 7,658 PointsMark Bradshaw
Courses Plus Student 7,658 PointsThanks Erwin. Cheers. I have more reviewing to do in this area. Thanks for posting the article Ryan.