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 Practice forEach in JavaScript Practice forEach forEach Practice - 7

Gloria Cho
PLUS
Gloria Cho
Courses Plus Student 278 Points

Why does this forEach code cause weird repeat and format of answer?

months.forEach(month => {
  capitalizedMonths.push(month.toUpperCase())
})

causes answer to come out as

[ 'JANUARY' ]
[ 'JANUARY', 'FEBRUARY' ]
[ 'JANUARY', 'FEBRUARY', 'MARCH' ]
[ 'JANUARY', 'FEBRUARY', 'MARCH', 'APRIL' ]
[ 'JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY' ]

instead of [ 'JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY' ]?

Seth Shober
Seth Shober
30,240 Points

Where is you're logging statement happening? Make sure you are logging after the forEach has completed.

1 Answer

Steven Parker
Steven Parker
231,236 Points

You don't need to do any output for the challenge, and the code shown here would not output anything.

But if you try this outside of the challenge, and add logging inside the loop, then what you show would be the expected output as you would see the new array being built up one step at a time.