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!

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

Michael Paccione
Michael Paccione
9,017 Points

Javascript Arrays Stage 4: Challenge 1 of 4

Challenge task 1 of 4

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.

I don't understand what this means. Any help?

This is code

      var spareWords = ["The","chimney","sweep's","dog"];
      var saying = ["quick", "brown", "fox", "jumps", "over", "the", "lazy"];
      var firstWord = spareWords;
      var lastWord = spareWords;
      saying;
      saying;
Michael Paccione
Michael Paccione
9,017 Points

I don't really understand the directions but I thought it wanted me to...

var firstWord = spareWords.unshift('firstWord');

2 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Michael;

Have you tried

var firstWord = spareWords.shift();

That should do what you are tasked to do.

Ken

Michael Paccione
Michael Paccione
9,017 Points

You are correct sir. Thank You!

I think the wording is strange on the instructions. I get the removing it from the beginning of the array - yea that sounds like shift. The set it to be the first word in array I don't really understand so much. How does shift set it to the first word... Is this just a semantics thing?

Hi Michael,

The instructions want you to do two things. It wants you to remove the string "The" from the beginning of the array and it also wants that string assigned to the variable firstWord

It turns out that the shift() method does 2 things. It removes the first element from an array and it also returns that value. This means that you can assign that return value to a variable. In this case, the return value "The" is being assigned to the firstWord variable.

I hope that helps clear it up for you.

You can read more about the shift() method here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift

Ken Alger
Ken Alger
Treehouse Teacher

Michael;

The shift() method in JavaScript removes the first item of an array, and returns that item, and reduces the length of the array

The unshift() method adds new items to the beginning of an array, and returns the new length, obviously lengthening the array.

Happy coding!

Ken

Russell Beye
Russell Beye
3,367 Points

This is asking you to take spareWords and splice the first word in the array (spareWord[0]) and place it in firstWord.

var firstWord = spareWords.splice(0, 1);

This splices the one element, starting at the 0 position of spareWords, and stores it in the firstWord.

Michael Paccione
Michael Paccione
9,017 Points

Okay this didn't work in the tutorial because they are looking for the .shift. To be fair your method probably works outside of the tutorial. I dunno I'm new at it so I am not familiar with splice yet.

Hi Russell,

The splice method doesn't quite work out here. It will correctly change the array but it returns an array of the removed elements.

It will return ["The"] instead of "The"

This means the firstWord variable will not have the correct value.

Russell Beye
Russell Beye
3,367 Points

I wasn't familiar with the problem set, and if he needed a string or an array as the returned type. As we showed, there is more than one way to skin a cat. :)