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

Jason Rich
seal-mask
.a{fill-rule:evenodd;}techdegree
Jason Rich
Front End Web Development Techdegree Student 10,048 Points

array.push() not pushing?

I need some help. I'm not grasping why the following in a for loop keeps creating new arrays with length of 1 instead of pushing onto the single array match. Or is it working as intended and I'm just unable to console.log() the full array?

const match = [];
match.push(getLetters[i].textContent);

And here is the result I'm getting with console.log(match); after selecting the "a" and "o" buttons.

["a"]
0: "a"
length: 1
>__proto__: Array(0)

["f"]
0: "f"
length: 1
>__proto__: Array(0)

1 Answer

Juan Luna Ramirez
Juan Luna Ramirez
9,038 Points

Can you post more of your code? If you are declaring match inside the loop then a brand new (empty) array is being created every loop iteration. Try declaring match outside of the loop.