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 - 1

Melissa Benton
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Melissa Benton
Front End Web Development Techdegree Graduate 17,230 Points

How 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?

app.js
const numbers = [1, 2, 3, 4, 5];
let total = 0;

// Write loop here:
numbers.forEach((number, index) => {
  total = number + ;
});

1 Answer

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,716 Points

You 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!