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 JavaScript Quickstart Arrays and Loops Create a forEach Loop

Using forEach, iterate over the numbers array and multiply each number by 5, storing these new numbers in the times5 arr

I'm confused, can you please help?

app.js
const numbers = [1,2,3,4,5,6,7,8,9,10];
let times5 = [];

// times5 should be: [5,10,15,20,25,30,35,40,45,50]
// Write your code below
numbers.forEach(

OK Thanx for your support!

2 Answers

Kendell Dancy
Kendell Dancy
15,158 Points

The forEach() method executes a function once for each array element.

We want to take the numbers array and multiply each element in that array by 5.

While also pushing the value to times5 array

numbers.forEach(function(number){
    times5.push(number * 5);
});
Mike Hatch
Mike Hatch
14,940 Points

Just to add to what Kendell said, think of it as passing a function in as an argument to numbers. To see the output: console.log(times5).

You could also write it like this :

numbers.forEach(number => times5.push(number * 5));
Sue Menon
Sue Menon
1,119 Points

Thanks Oskar! Your example helped me because that was the format they used in the video.

Could you just do that like this:

numbers.forEach(function(number) {
     times5 = number * 5;
});