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

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

I can't make the "Create a ForEach loop" coding challenge in JavaScript quickstart work... Can someone help?

I don't know how to store data in a new array, using a script. I have not learned this in the course.

It would be helpful to know for what purpose exactly you're creating the loop for. That way it'll be easier to give you the specific syntax and thought process behind it. But either way, what youre looking for should be fairly simply and executeable in just a couple lines code. This documentation on MDN should be pretty helpful and explanatory.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

Steven Parker
Steven Parker
231,153 Points

Please show the last code you tried, and include a link to the challenge course page.

4 Answers

Steven Parker
Steven Parker
231,153 Points

You're close, but the syntax around "push" isn't quite right. In particular:

  • you don't use indexing with "push"
  • assignment is also not needed
  • the value to be added should be passed as an argument (between the parentheses)
numbers.forEach(function(number) { times5.push(number * 5); });
Steven Parker
Steven Parker
231,153 Points

After giving it your best "good faith" try, using what you learned in the store data and forEach videos, what does your code look like?

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

this is my code:

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

and this is the error message Bummer: Looks like you haven't added any values to the times5 array. HINT: the push() method adds a value to the end of an array.

i dont no which value to give the push method...

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

Thank you so much... Really appreciate it!