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

JavaScript Quickstart create a forEach loop

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

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

My first answer: times5 = numbers.forEach(num => console.log(num * 5));

It ran in my text editor but not in the code challenge. It just logged 50 to the console not 5 to 50?

Second attempt: numbers.forEach(num => times5 = num * 5);

console.log(times5);

It logged 5 to 50 to the console but I have a feeling it didn't assign them to the times5 variable properly but why?

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

1 Answer

Alexander Besse
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Besse
Full Stack JavaScript Techdegree Graduate 35,115 Points

Hi Chris Roper!

I want to clarify some logic in your first and second attempts at the coding challenge. The objective of the task is to loop over all the numbers in the numbers array and then store the given number * 5 in the times5 array.

My first answer: times5 = numbers.forEach(num => console.log(num * 5));

The .forEach() array method doesn't return anything. The code attempt below will run the .forEach() loop but then set times5 to null when it's done.

Second attempt: numbers.forEach(num => times5 = num * 5);

This is much closer. You're looping over each element in the numbers array, good! But, you're setting times5 to the value of num * 5. Remember, we want to append the value of num * 5 to the times5 array - not set times5 to an integer value.

Your code should look like this:

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

Hopefully, that helps. Happy coding!

Great thanks! That makes much more sense now.