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 trialKiara Stewart
4,283 PointsWhere did I go wrong? For each array
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(function(){
times5 = numbers * 5;
});
Cameron Childres
11,820 PointsSaw 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
11,820 PointsHi 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.
Kiara Stewart
4,283 PointsOh, 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!
Kiara Stewart
4,283 PointsSo if I rewrote this in arrow for it would be -
numbers.forEach( item => times5.push(item*5));
Cameron Childres
11,820 PointsExactly, you got it!
Kiara Stewart
4,283 PointsKiara Stewart
4,283 PointsUpdate: (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);