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

Nicholas Lee
Nicholas Lee
12,474 Points

Methods Part 1 problems

This is what is causing me trouble, Methods Link

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

I originally went through each task successfully. Writing spareWords.shift(); spareWords.pop(); and each saying line. However, once I complete the last saying line of code it turns out bad. Tast 3 does not work, then task 1.

I looked up help with this already and it seems like some people agree, including myself, the task one and two need to be rewritten. They honestly make no sense.

Can anyone see what is wrong with my code?

9 Answers

Cassio Victor Cadore
Cassio Victor Cadore
6,579 Points

Nicholas, this is how the code looks by the end of the Challenge.

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

On tasks 1 and 2 the methods shift() and pop() remove the items from the array, while returning which item was removed. So here you are removing them, and while setting their value to the variables.

And the problem is actually in Task 4, when you used shift() instead of push().

:D

this code dont work it says "It looks like Task 1 is no longer passing."

Nicholas Lee
Nicholas Lee
12,474 Points

var firstWord = spareWords.shift(); var lastWord = spareWords.pop(); saying.unshift(firstWord); saying.push(lastWord);

just went through it again completing it with this code. he forgot a capital W in the last line !

John Doe
John Doe
Courses Plus Student 3,504 Points

saying.push(lastWord); is the correct answer to the last question.

You can't just copy and paste this answer as it will cause the other tasks to fail. Make sure you follow line-by-line, or task-by-task.

Alex Burdine
Alex Burdine
13,941 Points

saying.push(lastWord);

on part 4, he had the "W" as "w" and that's why you got an error.

Cassio Victor Cadore
Cassio Victor Cadore
6,579 Points

Nicholas, can you post what are the Tasks?

Nicholas Lee
Nicholas Lee
12,474 Points

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

Challenge task 2 of 4 On about line 19, use a method on the 'spareWords' to set 'lastWord' to be the last word of the 'spareWords' array whilst simultaneously removing it from the end of the array.

Challenge task 3 of 4 On about line 20, add the 'firstWord' variable to the beginning of the array 'saying'.

Challenge task 4 of 4 On about line 21, add the 'lastWord' variable to the end of the array 'saying'.

J Scott Erickson
J Scott Erickson
11,883 Points

One thing I see is the arguments you're passing in shift

// This returns the item from the front of the array and takes no arguments
Array.shift();
Nicholas Lee
Nicholas Lee
12,474 Points

For challenge 1 and 2 I am very confused. How do I even write "whilst simultaneously removing it from the beginning of the array" in the code to work?

thank you guys really needed this to move on

Jason Atkinson
Jason Atkinson
26,245 Points

wow i found this pretty impossible to understand too i think it really needs rewriting, has any one let them now?

var firstWord = spareWords.shift(); var lastWord = spareWords.pop();