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

Looking for help with forEach loop - code challenge

I am looking for a hint to solve the code challenge creating a forEach loop.

The task says:

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

My current code is:

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(number) {
  times5[number] = number * 5;
});

I get the error message:

"Bummer: Something's not right. Output of times5: [null,5,10,15,20,25,30,35,40,45,50]"

And I don't really get it why the first value in the created array is null.

Anyone can give me a hint?

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(number) {
  times5[number] = number * 5;
});

2 Answers

Cheo R
Cheo R
37,150 Points

It's because you're not inserting the results at index 0 in the times5 array.

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

Say number is 1.

1 * 5 is 5.

times5[1] has the value 5. The rest get inserted sequential. Arrays are zero-based.

Currently there's nothing at index 0. You get null.

Thanks a lot. I got it right now. :-)

Noah Ellekjær
Noah Ellekjær
2,739 Points

I Still can't get it right, would you mind sharing the correct answer?