Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Arrays Multidimensional Arrays Review JavaScript Arrays

Quiz Question 2 (What is the correct answer?)

Say you have two arrays, itemsA and itemsB, and you want to combine them into a new array named allItems. Complete the code so that the new allItems array stores the elements from itemsA first, followed by the elements inside itemsB:

const itemsA = [ 'Lightsaber', 'Mockingjay pin', 'Box of chocolates' ]; const itemsB = [ 'Ghost trap', 'The One Ring', 'DeLorean' ] const allItems = [ , ];

Abdi Salad
Abdi Salad
8,078 Points
const itemsA = [ 'Lightsaber', 'Mockingjay pin', 'Box of chocolates' ]; 
const itemsB = [ 'Ghost trap', 'The One Ring', 'DeLorean' ];
const alltems = itemsA.concat(itemsB);

this is how you would you do that. It's important to check out documentation. visiting the JavaScript array documentation would've saved you some time because it has useful methods there.

4 Answers

Thank you Abdi for your reply. I did google on how to combine the two arrays and I did find the answer that you provided to me but in this specific instance, it looks like the better solution was to use the spread operator to copy both arrays into the third array. That solved my challenge question. Thank you again for your reply.

I agree in fact, abdi answer is wrong given the problem being asked. it has a comma but abdi answer ignore the comma

...itemsA, ...itemsB

For this problem, we use the spread operator(...) to join the two arrays.

const itemsA = [ 'Lightsaber', 'Mockingjay pin', 'Box of chocolates' ];
const itemsB = [ 'Ghost trap', 'The One Ring', 'DeLorean' ]
const allItems = [ ...itemsA , ...itemsB  ];
Manoj Gurung
Manoj Gurung
5,313 Points

all the answers donot provide the real answer requested. We gotta change this.