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 trialJiecao Wang
5,833 PointsDon't understand where I am wrong
var saying = first.concat(second); saying.push("dog");
The above give me green light
however the following: var saying = first.concat(second).push("dog"); or var saying = (first.concat(second)).push("dog");
always give me red light, i don't get why the later two solution don't work
5 Answers
Jason Anello
Courses Plus Student 94,610 PointsHi Jiecao,
Your 2nd and 3rd statements are valid it's just that they're not working the way you are thinking.
first.concat(second)
does join the 2 arrays and returns that newly created array. You're then calling the push
method on that newly created array which would add "dog" to the end of the array. However, the push
method does not return that newly created array. Instead it returns the length of the newly created array which is 9. So in both your 2nd and 3rd statements you're assigning the number 9 to the saying
variable.
I think the challenge does want you to create one-liners here which I think was what you were trying to do.
The concat
method can take 1 or more arguments. This means you can do var saying = first.concat(second, "dog");
Daniel Wilson
5,929 PointsThose declarations wont work for two reasons: First, you cannot use .push before assigning a value to the variable 'saying'. This results in an error. It worked in the first declaration because you use a ; after assigning a value to 'saying'. Second, you have to specify that you want to modify the variable 'saying' with .push just like you did in the first statement (saying.push("dog"))
Jiecao Wang
5,833 Pointsuhh, totally forgot push doesn't return anything. Just learnt concat can take more than 1 parameter!
Jason Anello
Courses Plus Student 94,610 PointsHI Jiecao,
push
does return the length of the new array. It just doesn't return the array itself.
Jason Anello
Courses Plus Student 94,610 PointsI should have said that if push
didn't have a return value then your saying
variable would have been undefined rather than have a value of 9.
Jiecao Wang
5,833 Pointsyes the length of the new array
Jiecao Wang
5,833 Pointsyes the length of the new array