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 trialGloria Cho
Courses Plus Student 278 PointsWhy 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' ]
?
1 Answer
Steven Parker
231,248 PointsYou 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.
Seth Shober
30,240 PointsSeth Shober
30,240 PointsWhere is you're logging statement happening? Make sure you are logging after the
forEach
has completed.