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 trialAshley Scott
6,426 PointsHelp!!!!!!!!!!!!
The question says On about line 20, add the firstWord
variable to the beginning of the array 'saying'.
I have
var spareWords = ["The","chimney","sweep's","dog"];
var saying = ["quick", "brown", "fox", "jumps","over", "lazy"];
var firstWord = spareWords.shift();
var firstWord = spareWords.shift();
var firstWord = spareWords.unshift();
What am I doing wrong?
4 Answers
Kevin Lozandier
Courses Plus Student 53,747 PointsHi, Ashley Scott :
I finally found the challenge, and the following is the correct answer that'll I'll break it down for you:
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);
The challenge initially asks you to get and remove at the same time the first item in saying
to be assigned to firstWord
. It then asks you to get and remove the last item in the array at the same time after doing that:
var firstWord = spareWord.shift();
var lastWord = spareWord.pop();
Now the challenge is asking you to insert at the beginning of saying
array the item stored in firstWord
. To do that in JavaScript you use the unshift
method that prepends. You must pass in explicitly firstWorld
because what's asked is a variable not a new string not contained in a variable:
saying.unshift(firstWord);
From there you're asked to add the string contained in the lastWord
variable at the end of the saying
array. To do that, you use the push()
method.
saying.push(lastWord);
With these things done. You should be able to pass the challenge just fine. Does my explanation clear things up for you, or would you like me to explain this in more detail?
Kevin Lozandier
Courses Plus Student 53,747 PointsHi, Ashley:
I'll be more than happy to help you. What problem is this for what course?
Assuming your firstword
variable is supposed to take the firstword of spareWords
to then prepend to the saying
array, currently you're making your firstword
variable have the first and second items of the spareWords
array, and then prepending to spareWords
yet again with nothing (wouldn't be surprised that's an error).
You're never actually prepending anything to the saying
array.
var spareWords = ["The","chimney","sweep's","dog"];
var saying = ["quick", "brown", "fox", "jumps","over", "lazy"];
var firstWord = spareWords.shift(); // assigns "The" to firstWord with the word removed from spareWords
var firstWord = spareWords.shift(); // assigns "chimney" to firstWord with the word also removed from spareWords
var firstWord = spareWords.unshift(); // you're prepending nothing (???) to spareWords.
Ashley Scott
6,426 PointsChallenge task 3 of 4 in Stage 4 Arrays in JavaScript Foundations
Ashley Scott
6,426 Pointsvar spareWords = ["The","chimney","sweep's","dog"];
var saying = ["quick", "brown", "fox", "jumps", "over", "the", "lazy"];
var firstWord = spareWords.shift();
var lastWord = spareWords.pop();
var firstwor = saying.unshift;
saying;
This what i have and it keeps saying:
Bummer! You're missing the 'unshift' method and the 'firstWord' variable.
What am I doing wrong?
Kevin Lozandier
Courses Plus Student 53,747 PointsBased on the code sample you've provided, you've misspelled firstWord
, you didn't actually call unshift
on the saying
variable, and left saying
by its lonesome.
As a result, the code challenge is correctly informing you of your mistakes.
Ashley Scott
6,426 PointsAshley Scott
6,426 PointsThank you that helped!