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

Hi there, I've been stuck on this for a while and would love some help. Thanks

Hi there, I really don't know how to get going with this and would love to see a solution in order to work backwards and figure out how to get there. Thank you Rebecca

app.js
const numbers = [1,2,3,4,5,6,7,8,9,10];
let times5 = [5,10,15,20,25,30,35,40,45,50];

numbers.forEach(*5(times5){alert(times5);
// times5 should be: [5,10,15,20,25,30,35,40,45,50]
// Write your code below

9 Answers

Steven Parker
Steven Parker
231,236 Points

Let's try going forwards by breaking the task down into steps. First, don't change the original declaration of "times5" that creates it as an empty array. Then, the code that calls "forEach" is good, but take a closer look at what is being passed as the argument :point_right:*5(times5){alert(times5

This isn't a complete expression, but what should be there:

  • the argument should be a function that takes an argument (which will be the current number)
  • the code inside the function should multiply the current number by 5
  • the result should be stored in the "times5" array (perhaps by using "push")
  • you won't need "alert"

Give it another try with this in mind. I bet you can get it! Write again if you have trouble.


UPDATE: So you wrote "*5 (number)" to "take each number and times it by 5". You're right that the "*" symbol is the operator for multiply. But the syntax for math symbols is that they go between the two items they will operate on.

Then the next issue is that "forEach" doesn't take an expression for an argument, it needs a function. You can see an example towards the end of the Simplify Repetitive Tasks with a forEach Loop video. But you've probably also seen how to write a function in other courses as well.

Then using the "push" method to add things to an array is covered about 2½ minutes into the Store Data with Arrays video. The challenge has already created the array for you, empty to start with.

Steven Parker
Steven Parker
231,236 Points

You are correct in that the "push" method applied to these numbers would be adding more numbers onto the end, and that's another way to describe what that task asks for when it says "storing these new numbers in the times5 array.".

Your question about "what the user is going to see" might explain part of the confusion. This particular exercise doesn't display anything that you might see, it only affects variables.

And "times5" is the name of the variable that contains the new array. The "push" method should be called on the array being added to, so the code would be like "time5.push(/*value_to_be_added*/)".

And one final hint is to be careful of the function syntax. The declaration should include only the name of the parameter, any code that works on the parameter should go inside the braces.

Hi Steven,

Thanks so much for your help. I am a beginner so I'm not really understanding what this exercise is asking me to do so getting go with the coding seems like a shot in the dark. I am needing to go backwards to recap the learning but this exercise won't let me go back so am trying to hash my way through, I'm so sorry for my incompetence!! This is what I've tried to put together so far...

forEach(numbers (times5); {array.push(times5)}

Any closer? Appreciate your help so much!

Bex x

Steven Parker
Steven Parker
231,236 Points

Closer, but you still don't have a function there.

You can go back by clicking on the other circles on the progress bar that goes across the top of the page. The current page is represented by the circle with the ring around it.

If you need to go back to a previous stage, you can go directly to the course index using the link made by the course name at the top left (above the bar with the circles). From there, you can go directly to any page in the course.

Thanks Steven.

Went back and did all the videos again, however I am stilll really struggling and getting quite frustrated!

Was wondering if you could help me with the solution?

This time I have...

numbers. forEach (*5 (number)){

Which as far as I can tell would take each number and times it by 5? The function being to multiply the 'number' by 5.

It is then putting it into a separate array that I just can't my head round.

Thanks so much! x

Thanks so much for your response Steven. I'm sure this will make make so much sense once it all clicks and I look back on our conversation here.

I understand about the times 5 situation. So within those brackets it should read...

(number) *5) {

Just watched the Simplify Repetitive Tasks with a forEach Loop video again, and know how to write a function but in this situation I really don't know where to start. I also don't understand why a push method is needed to get the outcome this task is asking for?

Would you mind sending me through the correct solution and then I can start to understand it and move on with the course. I only have 2 more days to complete this and I'd like to move on, but am stuck as I can't get passed this task.

Thank you so so much

Bex

Hi Steven,

Could this be correct for the part involving the function....

numbers. forEach (function (number *5)){

Also understand that the push method applied to these numbers would be adding more numbers onto the end, but the task doesn't ask for this? It asks for us to store them in a separate array? I think my issue on this taks is that I don't understand what the tasking is asking us to do.

iterate over the numbers array = be able to access each element within the array individually

And then times these by 5.

I understand that so far. Now the next part where it says to store these new numbers in the times5 array is where I get lost. Where are they stored? I just don't get what the user is going to see and so I'm struggling to imagine what I need to achieve.

numbers.push (times5)??

Don't really understand where 'times5' is coming in as a variable?

I'm so so sorry for this and so grateful to have someone helping me. Without some guidance this feels like banging my head on a brick wall haha!!!

Thanks so much Steven x

Thanks Steven..

Would you mind typing out the answer for this task for me?

I feel like I'll understand So much more once I can see where I need to get to, Then I'll go through it with a fine tooth comb with your comments to make sure I know why that is the solution.

Thanks

Steven Parker
Steven Parker
231,236 Points

I'd feel like I've done a better job by guiding you to resolving it yourself. You must be really close at this point, show the code you have after applying the last set of hints and there's probably just one point or two left to getting it working.

Thanks so much Steven! really appreciate it x

Steven Parker
Steven Parker
231,236 Points

So have you resolved the issues? If not show the code at the current point.

A friend helped me with giving me the answer so now i know the layout of what it should be.

numbers.forEach(function(number) { times5.push(number * 5) }); console.log(times5);

I think it's just repetition of seeing the layout and getting familiar with the syntax. Thanks so much for all your help x