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 Array Iteration Methods Array Iteration Practice forEach()

Well this is new for me and until I get used to it, it will last a while. Help me solve this challenge...

Well I understand the overall idea but it's complicated until I memorize the functionality of the process. I don't understand if I should add name.forEach (); as a new code line or if I should add it inside the times10 brackets ? Anyway I can't seam to figure it out this way, can you help me ? Html & CSS are more easily to understand it seam in comparison only. Thank you for you help !

app.js
const numbers = [1,2,3,4,5,6,7,8,9,10];
let times10 = [for];

// times10 should be: [10,20,30,40,50,60,70,80,90,100]
// Write your code below
Steven Parker
Steven Parker
231,248 Points

FYI: Be aware that "Java" and "JavaScript" are different languages (despite the name similarity).

2 Answers

Lets break the base code down a little.

// declare a constant variable with a reference in memory named numbers, and 
// assign that variable the value of an array with the given array values.
const numbers = [1,2,3,4,5,6,7,8,9,10]; 

//declare a scoped variable with a reference in memory named times10, and
// assign that variable the value of an empty array
let times10 = [];

So, we have two variables, each representing arrays. the times10 array is currently empty.

There are 2 steps to this problem. First, for each stored value in the numbers array, we need it's multiple of 10. Second, we need to store these multiples of 10 in the times10 array.

After some research, we discover that the forEach method takes an anonymous function, and that the parameter passed into the anonymous function represents the iterated value during each iteration of the array.

Let's solve the first problem.

numbers.forEach(function(number){
   //First
   let multiple = number * 10;
})

The above code iterates over each value in the numbers array. with each iteration, it assigns the scoped variable of multiple the value of the given number multiplied by ten. Let's solve step two and make sure we store this somewhere.

after some more research on how to add values to an empty array, we discover that arrays have a push() method that can help us.

numbers.forEach(function(number){
   //First
   let multiple = number * 10;

  //Second
  times10.push(multiple)
})

Booyah! How do you like 'dem apples?

Steven Parker
Steven Parker
231,248 Points

...or perhaps you just wanted a spoiler. :smirk:

Thank you for taking the time to answer my question, in fact I found out I needed to start with the beginners java course and I did so as a result the website suggested this course for java. When I will reach this level I will take notice of your answer. Thank you!

Steven Parker
Steven Parker
231,248 Points

FYI: Be aware that "Java" and "JavaScript" are different languages (despite the name similarity).

Steven Parker
Steven Parker
231,248 Points

You had it right the first time. Add new code below the line that says "Write your code below". Don't change the code that is provided to start with.

Hint: instead of "name.forEach()" you will use "numbers.forEach()" (nothing called "name" has been defined).

Hopefully you can get it now without a spoiler.