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

Challenge task 1 of 4 - How come my code isn't working?!

I'm on Javascript Foundations, Arrays, Methodfs Part 1 - elements, and I'm doing Challenge task 1 of 4.

They want me to use a method to simultaneously replace the first work with "firstWorld" and remove that "firstWorld". I don't understand why my code isn't working. Help please, it's been an hour.

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

spareWords[0]="firstWord";
spareWords.shift();

</script>

4 Answers

The task is asking you to set the variable firstWord to the first item in the array spareWords using a method.

The shift method returns a value, it returns the item that is being removed.

So, in one go you can do something like this:

var someFirstItem = someArray.shift();
var firstWord = spareWords.shift();
Alexander Smith
Alexander Smith
8,159 Points

They are asking you to set the "firstWord" variable to the first word of the "spareWords" Array and remove the first word of the "spareWords" Array at the same time. You are correct in using .shift().

Thanks guys I finally solved it

  • it was much easier than I thought.