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 trialMelissa Benton
Front End Web Development Techdegree Graduate 17,805 PointsHow to add elements together using forEach
I'm struggling to complete this forEach practice. How do I write the logic to add the previous number to the next number in the array?
const numbers = [1, 2, 3, 4, 5];
let total = 0;
// Write loop here:
numbers.forEach((number, index) => {
total = number + ;
});
1 Answer
Jeff Muday
Treehouse Moderator 28,722 PointsYou are so close!
If you try this construct it will work:
numbers.forEach((number, index) => {
total += number;
});
Good luck with JavaScript, it is an amazing language!