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

Array Method Part 1, Objective 1

I have tried these multiple times. It works in my workspace. Howver Cannot get this right in the Challenge. Please help.

var spareWords = ["The","chimney","sweep's","dog"]; console.log(spareWords) var firstWord = spareWords.unshift("firstWord"); console.log(spareWords) var secondWord = spareWords.shift();

console.log(spareWords)

4 Answers

Julian Gutierrez
Julian Gutierrez
19,201 Points

This is the solution to the full challenge (if we're talking about the same challenge)

var spareWords = ["The","chimney","sweep's","dog"];
      var saying = ["quick", "brown", "fox", "jumps", "over", "the", "lazy"];
      var firstWord = spareWords.shift();
      var lastWord = spareWords.pop();
      saying.unshift(firstWord);
      saying.push(lastWord);

Hi Julian,

Unfortunately id did not work. I have done the following as I understood from the challenge. It still did not work.

Thanks.

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

var firstWord = spareWords.unshift("firstWord"); console.log(spareWords) spareWords.shift();

console.log(spareWords)

Julian Gutierrez
Julian Gutierrez
19,201 Points

To get the first word of the array you would use the .shift method on spareWords. I believe the lesson is asking you to get the last element of the array not the second. You would use the pop method to retrieve the last element of the array.

var spareWords = ["The","chimney","sweep's","dog"];
      var saying = ["quick", "brown", "fox", "jumps", "over", "the", "lazy"];
      var firstWord = spareWords.shift();
      var lastWord = spareWords.pop();

Thanks it worked. appreciate : )