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

ryankite
ryankite
8,265 Points

Possible bug, or please correct me

Challenge: 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.

Error message: Bummer! The 'firstWord' should be the string of 'The'. It should be shifted from the 'spareWords' array.

my Solution:

var spareWords = ["The","chimney","sweep's","dog"];

spareWords.unshift("firstWord"); //adds "firstWord as the first element" console.log(spareWords.toString()); // result: firstWord,The,chimney,sweep's,dog

spareWords.shift(); //removes "firstWord as the first element" console.log(spareWords.toString()); //result: The,chimney,sweep's,dog

please explain why I'm not passing this challenge?

Thank you.

Ryan

2 Answers

Laura Cressman
Laura Cressman
12,548 Points

Hi Ryan, The question is asking you to set the value of the variable "firstWord" to be the first word of the spareWords array, while also removing the first word from spareWords. This is the shift method. To do this, you only need to change the code on line 18, to var firstWord = spareWords.shift();. No other code changes are needed. When you don't set the result of calling the shift method on spareWords to a variable, that word is simply deleted. Hope that helps :) Sunshine and happiness, Laura