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

Where did I go wrong? For each array

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(function(){
   times5 = numbers * 5;
});

Update: (it's still wrong lol)

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(num => num * 5);

Cameron Childres
Cameron Childres
11,820 Points

Saw your update after I posted -- you're on the right track! You just need to add in the .push method to get them added to the new array.

1 Answer

Cameron Childres
Cameron Childres
11,820 Points

Hi Kiara,

The function inside forEach needs a parameter. You can name it anything you like, it will represent the individual items inside the array. From there you will want to add them one at a time to the the times5 array which you can do using the .push method:

numbers.forEach( function(item) {
  times5.push(item*5);
});

Hope this helps! Let me know if you have any questions.

Oh, interesting! I thought you could only use .push to add items to the end of an existing array (with values). I never thought about using it in an empty array! Thanks for the advice!

So if I rewrote this in arrow for it would be -

numbers.forEach( item => times5.push(item*5));