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

Javascript Arrays Shift & Unshift

The question asks us to "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.".

The code that I have doesn't seem to work.

    <script>
      var spareWords = ["The","chimney","sweep's","dog"];
      var saying = ["quick", "brown", "fox", "jumps", "over", "the", "lazy"];
      var firstWord = spareWords.unshift('firstWord').shift('firstWord');
      console.log(spareWords.toString());
      var lastWord = spareWords;
      saying;
      saying;
    </script>

When running this through the browser I see Uncaught TypeError: Object 5 has no method 'shift' index.html:18 (anonymous function)

Can we not string together methods? I'm even further confused because when I add simply

      var firstWord = spareWords.shift();

I get an error that says I'm missing the shift method. Please help.

5 Answers

i think the instruction needs to be rephrased as i saw i wasn't the only one who got confused. /* use a method on the 'spareWords' to set 'firstWord' to be first word of the 'spareWords' array / - looks like you asked us to insert the new string ('firstWord') into the array - which naturally should use unshift method / whilst simultaneously removing it from the beginning of the array.". */ then removing that newly inserted array from the beginning of the list - using shift method

Kenan Memis
Kenan Memis
47,314 Points

Correct!!

The instruction has to be rephrased since it is confusing. .shift() works perfect but I do not think that just using shift() method is what the instruction asks.

Hi Michael! I believe you can't string together methods. The correct way is just to use shift since it will take off the first value of the array. I used var firstWord = spareWords.shift(); and it worked for me. Hope this helps!

Jacob Miller
Jacob Miller
12,466 Points

You can string together methods, but Denisse is correct that you only need to use .shift(), because it simultaneously removes the first item from an array and returns that item. Not sure why you were getting that error when you used only .shift(); it's working fine for me.

Yeah, this is weird—I feel like the wording could be much, much clearer, because it's actually making us create a variable called firstWord that also is not the first word of anything.

Code from the challenge again:

 var spareWords = ["The","chimney","sweep's","dog"];
      var saying = ["quick", "brown", "fox", "jumps", "over", "the", "lazy"];
      var firstWord = spareWords.shift(); 
Mahri Bahati
Mahri Bahati
2,254 Points

The wording is very confusing. I started off trying to add "firstWord" as the first item in spareWords, and couldn't figure out why or how I was supposed to remove it at the same time. It was apparently much simpler than that. I think it would have been more clear if it said "set the value of 'firstWord' to be the first item of the 'spareWords' array while removing that item at the same time."

Thanks for the guidance!