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

Can anyone help with the .shift code challenge? Can't seem to get it to run. It is passing my .unshift.

Methods : part 1 - can't get the .shift to run and I don't know what I am doing wrong. I have been able to do it in the console but just can't get it to work. Anyone nudge in the right direction would be great. Thanks!

index.html
<!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"];
      spareWords.unshift(firstWord);


      var firstWord = spareWords;
      var lastWord = spareWords;
      saying;
      saying;
    </script>
  </body>
</html>

3 Answers

Stefan Osorio
Stefan Osorio
16,419 Points
  • You are using unshift instead of shift. Shift removes the first item of the array on which it is called and returns it, while unshift puts the stuff in its parenthesis into the array.

  • The line "var firstWord = spareWords;" is meant to be a starting point for you to edit (the assignment says "On line 18[...]"). If you leave it in after you do the shifty stuff, it will assign the entire array spareWords to firstWord, overwriting your previous work.

Alex Heil
Alex Heil
53,547 Points

hey Jon Willey , by looking at your code and comparing with the challenge in task1 there are currently 2 probs. just for context this is the task: 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.

okay, let's figure it out ;)

1) you added a new line

spareWords.unshift(firstWord);

to the code, but the task wants you to alter the existing piece at line 18, so we should remove the new code here and instead fix the pre-written code

2) on line 18 we have

var firstWord = spareWords;

So we need a method now to get the the first word out of spareWords, do you remember what we're using to do this? I guess you know it, it's the shift()-method. Now that we know what we need to use let's add it to the code, like so:

var firstWord = spareWords.shift();

with all this in place the code looks fine now and the challenge should pass just fine. hope that helps and have a nice day ;)

thanks Alex and Stefan.. I see what I was doing wrong.
I wasn't adding to the existing line. I appreciate the help.