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
Peaches Stubbs
21,320 Pointsshift / unshift method
The question is: 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.
This is the original code:
<!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"];
var firstWord = spareWords;
var lastWord = spareWords;
saying;
saying;
</script>
</body>
</html>
I have tried this a few times a few different ways and just can't seem to get the correct answer.
My last attempt was:
<!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"];
var firstWord = spareWords;
spareWords.unshift('firstWord');
console.log(spareWords.toString())
var value=spareWords.shift();
console.log(spareWords.toString())
var lastWord = spareWords;
saying;
saying;
</script>
</body>
</html>
I am not sure what I am doing wrong and any help will be greatly appreciated as I want to be sure that I learn this concept properly.
The link is: http://teamtreehouse.com/library/websites/javascript-foundations/arrays/methods-part-1-elements
It keeps saying that I am not adding the shift, even when I use both.
4 Answers
Pedro Cabral
Full Stack JavaScript Techdegree Student 23,916 PointsIt's a bit harsh to read the code if you don't format your post - solution is as follows:
var firstWord = spareWords.shift();
Chase Lee
29,275 PointsPeaches Newbold I reformatted your code. You can find out how I did it here.
Andrew Garcia
1,697 PointsInstructions on these need to be more clear! var spareWords = ["The","chimney","sweep's","dog"]; var saying = ["quick", "brown", "fox", "jumps", "over", "the", "lazy"]; var firstWord = spareWords.shift(); spareWords.unshift('firstWord'); console.log(spareWords.toString()) var value=spareWords.shift(); console.log(spareWords.toString()) var lastWord = spareWords; saying; saying;
Fernando Teofilo
7,942 Pointssolution:
var firstWord = spareWords.shift();
var lastWord = spareWords.pop();
Peaches Stubbs
21,320 PointsPeaches Stubbs
21,320 PointsThank You, this worked at first , then I got to challenge 3 and this answer became void.