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 trialDionte Barnes
10,140 PointsUsing 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?
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(
2 Answers
Kendell Dancy
15,158 PointsThe 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
14,940 PointsJust 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)
.
Oskar Brännström
7,511 PointsYou could also write it like this :
numbers.forEach(number => times5.push(number * 5));
Sue Menon
1,119 PointsThanks Oskar! Your example helped me because that was the format they used in the video.
David Ryan
Courses Plus Student 14,981 PointsCould you just do that like this:
numbers.forEach(function(number) {
times5 = number * 5;
});
Dionte Barnes
10,140 PointsDionte Barnes
10,140 PointsOK Thanx for your support!