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

George Drakoglou
George Drakoglou
7,969 Points

Arrays method's part 1 challenge:elements (new)

I was rewvewing a little javascript and I saw some new challenges. All of the was easy but I stuck in one. It says

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.

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

I quite didn't understand the deal. To put something on the beginning of an array we use unshift amd to remove it we use shift.

I did something like

var firstWord = spareWords.unshift() or .shift() but I quite didn't know what to put in the parenthesis;

Marvin Abella
Marvin Abella
6,378 Points

You leave the parenthesis empty since its a default (0) it removes the first element in the array.

var firstWord = spareWords.shift(); dont forget the semi-colon, i had problems with this as well.

Adam Ruf
Adam Ruf
10,638 Points

Thanks, George & Marvin!

Glad I'm not the only one who got stuck on this one.

I think this question was poorly worded and needs updating. It sounded like it was asking us to use the .unshift method to add the string "firstWord" to the array and simultaneously use the .shift method to remove it (Which is kind of a weird request).

More helpful wording for this challenge might be something like, "For the variable "firstWord", use a method to remove the first word from the beginning of the "spareWords" array.

Adam Ruf

Same

Juliano Vargas
Juliano Vargas
Courses Plus Student 15,575 Points

I am having the same issues with this task it seems that we are not understanding what they want us to do or we are not interpreting well or the question is badly elaborated anyone know how the code to pass this task?

Juliano Vargas
Juliano Vargas
Courses Plus Student 15,575 Points

Ok here is what i did to pass this task use this : var firstWord = spareWords.shift(); and forget about unshift(); all it does is remove "the" from the array spareWords so the does the question make sense to anybody?

Joga Gavidia
Joga Gavidia
4,047 Points

The question is confused, have to be better explained!

13 Answers

Mike Rolih
Mike Rolih
9,766 Points

All I did to pass this challenge was to omit the .unshift() method and just used the .shift() method like this: var firstWord = spareWords.shift(); and this should work for you. This question is poorly written and really confusing and should be revisited by Treehouse.

I agree, the wording needs improvement. Thanks

danieldeepak
danieldeepak
9,934 Points

I do agree, they need to rephrase their questions. I spend more time trying to figure out poorly written questions than code.

Enny Benzonelli
Enny Benzonelli
6,479 Points

I agree. The question is really confusing. It should be reworded.

Juliano Vargas
PLUS
Juliano Vargas
Courses Plus Student 15,575 Points

Ok here is what i did to pass this task use this : var firstWord = spareWords.shift(); and forget about unshift(); all it does is remove "the" from the array spareWords so the does the question make sense to anybody?

Marvin Abella
Marvin Abella
6,378 Points

glad I helped =D goodluck!!

Marvin Abella
Marvin Abella
6,378 Points

George you should pick the best answer so this would be labeled as answered.

Karthikeyan Palaniappan
Karthikeyan Palaniappan
5,271 Points

Thanks Marvin, I too fell in this trap. The question is little tricky to understand.

Marvin Abella
Marvin Abella
6,378 Points

Honestly I had to re-watch the video a couple times to figure it out. It just takes test & trial, that's how we learn. Good luck on your ventures!

I am having some issues with part 2 of this task....where it asks to set the last word in the array and then remove it from the array....

var spareWords = ["The","chimney","sweep's","dog"];
      var saying = ["quick", "brown", "fox", "jumps", "over", "the", "lazy"];
      var firstWord = spareWords[0];
      sparewords.shift();
      var lastWord = spareWords[2]; //as "The" has been shifted out??????
        spareWords.pop();

Anybody got round this?

"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 way the question is worded you would` think Treehouse wants firstword to be added to the array, then removed from the array. It should say something like assign 'firstWord' the first value of the array 'spareWords' whilst simultaneously removing the first value from the array.

Andy Bradbury
PLUS
Andy Bradbury
Courses Plus Student 4,697 Points

Adding my name to the list and I agree, I did exactly the same and tried the following:

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

Didn't work, so then:

var firstWord = spareWords.unshift("firstWord").shift(); (which also didn't work, obviously)

Then I finally Googled it and found this.

Thank's everyone. Once you get the first is the same on the rest... just "ignore" the first part of the task... i guess, cause it's confusing, i thought it was to add/take those words - 'fristWord', etc... - to the end/ beginning of the array :)

var firstWord = spareWords.shift();

It's still not reworded : (

I really struggled to understand this question...lol. Thanks for this thread.

Wow, agreeing with everyone above, this is a terribly worded question.