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

Doing math on each array item

Hi,

Looking for some guidance, I have an array populated with some figures from JSON data (all whole figures) what I am struggling to do is run some math on each item in the array and either have this change the array or create a new one. I can do it one by one like so but would like to implement it with FOR or EACH not really sure.

var name = pointsArray[1] / total * 100;

Then do this for each item but want to try be as minimal code as possible thanks for any advice

1 Answer

I'd go with the FOR loop. Something like:

for (var i = 0; i < 25; i = i + 1) {
   pointsArray[i] = pointsArray[i] / total * 100; 
 }

Is that what you're looking for?

Cheers think just sorted it like so

pointsArray.push(Math.round(val / total * 100));

seems to be working ok.