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

Chris Shaffer
Chris Shaffer
12,030 Points

I don't understand WTF you're looking for here.

This is extremely confusing. It seems to be suggesting to make the variable "var firstWord" to be equal to "spareWords.shift("firstWord") which is exactly what I did, but it says I didn't the shift method, which I obviously DID.

Way to word this in the most confusing possible manner.

You are totally right. Whoever sets the instructions for the js exercises is either extremely arrogant and wants to make it seem more difficult than it is or is just stupid.

7 Answers

Chris Shaffer
Chris Shaffer
12,030 Points

I just figured this out. It's because this lesson is worded incorrectly. It specifically says "use a method on the 'spareWords' to set 'firstWord' to be first word of the 'spareWords' array" and then remove it again.

To set "firstWord" to be the first word of the array and then immediately remove it (according the video that precedes this quiz), you have to use arrayName.shift("string"); if you want to simply add to the beginning, then you would use arrayName.*un*shift("string").

Now, as is done in the video, the value is added with .unshift(value) and removed again by using .shift() with no value passed.

But that's not what the instructions request. .shift (again, according to your video) can be used (and I've tested and verified in Sublime Text just fine, as well as the browser) to pass in a value and immediately remove it again.

This does not pass on your quiz if the value is assigned to the variable, as instructed, and it doesn't pass with JUST .shift.

What passed for me was this:

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


      spareWords.unshift("firstWord")
      spareWords.shift()

I also tried NOT including the .unshift method, but received the same Bummer message.

Luciano Oliveira
seal-mask
.a{fill-rule:evenodd;}techdegree
Luciano Oliveira
Front End Web Development Techdegree Student 11,548 Points

I have to agree with Chris. This question is very poorly written and make us spend unnecessary time trying to figure it out! come on treehouse, let's put a little more thought into these questions!

Chris,

I think you're getting yourself too worked up about this.

First of all, the shift() method doesn't accept any arguments so you shouldn't be putting anything between the parentheses. It makes sense if you think about it. This method simply removes the first element from an array. It doesn't need anything passed in to do that.

On the other hand, the unshift() method adds elements to the beginning of the array so it does accept arguments because it needs to know what exactly it is adding to the beginning of the array. So whatever you pass in is what gets added to the beginning of the array.

Here's the instructions: 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.

So they want you to do two things here. First, the variable firstWord needs to be assigned the first word from the spareWords array. So when it's all done firstWord should have the value "The". Secondly it wants that first word in the array removed from the array.

In other words, the word "The" should be removed from that array and it should also be assigned to the firstWord variable.

Out of the four methods, shift, unshift, pop, and push, we want shift because that's the one that removes the first element from an array.

The .shift() method does two things. It removes the first element from the array and it also returns that value. So it will remove the word "The" from the array and also return that value. Which means we can use that return value in an expression or in this case we are assigning that return value to the variable firstWord.

Put another way, spareWords.shift() is going to evaluate to "The" which is then assigned to the variable on the left side of the assignment statement.

The code:

var firstWord = spareWords.shift();

I hope that helps.

Chris Shaffer
Chris Shaffer
12,030 Points

So, if it's not clear to a mod or someone else reading this, this quiz question is broken based on the starting setup and instructions.

Either method we've listed above works to pass it, but it won't be logical to another user, nor does it actually meet the requirements set by the instructions.

I think either the validation check or the instructions need to be changed.

Tommy Gebru
Tommy Gebru
30,164 Points

here try this instead it worked for me,

<!DOCTYPE html>
<html lang="en">
  <head>
    <title> JavaScript Foundations: Arrays</title>
    <style>
      html {
        background: #FAFAFA;
        font-family: sans-serif;
      }
    </style>
  </head>
  <body>
    <h1>JavaScript Foundations</h1>
    <h2>Arrays: Methods Part 1</h2>
    <script>
      var spareWords = ["The","chimney","sweep's","dog"];
      var saying = ["quick", "brown", "fox", "jumps", "over", "the", "lazy"];
      var firstWord = spareWords.shift();
      var lastWord = spareWords;
      saying;
      saying;
    </script>
  </body>
</html>
Chris Shaffer
Chris Shaffer
12,030 Points

Except this is actually WRONG based on the instructions. The instructions specifically ask you to replace the first word of the spareWords array with "firstWord" and then remove it again.

Currently, the value of the variable firstWord is the array spareWords, meaning you can't simply make the value of the variable the first word in the array. You'd essentially be filling the array WITH the array's current contents.

If the value of var firstWord was, say "a string", then you could do this, but it's not.

What YOU did was remove the EXISTING first word, NOT replace it, and then you put it back again.

That's not what the instructions say, at all.

Teame Gebru has posted the correct code based on the instructions.

var firstWord = spareWords.shift();

What this code does is remove the first element from the spareWords array and assigns that removed element to the variable firstWord

This is exactly what the instructions are asking for.

Tommy Gebru
Tommy Gebru
30,164 Points

can you supply your code with instructions+bummer!message?

Chris Shaffer
Chris Shaffer
12,030 Points

These are the instructions:

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.

This is what I did:

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

The only thing I changed was "spareWords.shift("firstWord")". I've tried every combination of shift and unshift, including both at the same time. It does not accept the answer.

Bummer! You're missing the 'shift' method

Obviously, I'm not. I even tried this in Sublime Text and it works fine. I defined two variables, firstWord and lastWord and set the first to the same as I have here and the 2nd the same except w/unshift.

Tommy Gebru
Tommy Gebru
30,164 Points

Weird. Dont let unseen problems stop you, keep up the good work!

Also to close out a forum discussion, select Best Answer, this will help other students find similar solutions and keep the forum organized. Hope this helps.

Chris Shaffer
Chris Shaffer
12,030 Points

To close this out, I selected my own answer; my last comment above this one. This isn't due to ego, rather that it actually includes the details needed to pass this.

Also, I see a number of explanations here which seem to provide a "correct" answer, however don't pass the challenge.

The unfortunate issue, and the part that frustrates me (and clearly others) is that you ultimately LEARN nothing from the "secret" of passing this challenge.

The fact remains there is multiple ways of fulfilling the request as detailed in the question. The unfortunate fact is that this challenge is created in such a way that the method of passing is essentially trial and error. This is yet another unfortunate side effect of instruction by Jim Hoskins: http://teamtreehouse.com/jim . Nothing against him personally, but it seems the vast majority of the JS courses as instructed by him lead many down a path of confusion.

For what it's worth, I left Treehouse to study JS at Codeschool.com and returned only after I saw that the newer JS courses were done by Andrew Chalkey who clearly outlines the different methods and best practices, rather than spending 5 minutes telling you how you COULD do something then telling you not to do it that way only to spend the last minute of the video showing you the real way.