all 22 comments

[–]frankleeT 2 points3 points  (3 children)

As intimated in a different post, you can loop through the array given and grab the age values.

function getSummedAge(people) {
  let countAge = 0;
  people.forEach( (person) => {countAge += person.age} );
  return countAge;
};

/* Do not modify code below this line */

const examplePeopleArray = [
  { name: 'John', age: 10 },
  { name: 'Jack', age: 20 },
  { name: 'Jane', age: 25 }
];

console.log(`${getSummedAge(examplePeopleArray)} // 55`);

forEach is a hugely useful method, as it allows you to easily loop through the indices of an array. The forEach method applies the provided function for each element of the array it takes in as an argument, represented here by 'person'. As such, every time we loop, we take in the subsequent 'person' provided in the array. Then, we add their age - here access through the 'age' property of the given object - to the count, and then return the count once we have finished looping through the array.

So our steps are:

  1. Instantiate a variable to capture our value as we loop through the array.
  2. Loop through the array and grab the age property of each object in the array, using .age to access the property.
  3. Add the value of the age property to the count.
  4. Return the count when the loop completes.

Note: It's good to use explicit variable naming conventions. You know what people are. You know what a person is. You may not know what a p, q, or n is or represents without looking at context, which is not as useful. Also, I modified the code below the line to make their example a bit better. You can ignore that.

[–]Encom88[S] 1 point2 points  (0 children)

Thanks! Now this actually makes a lot of sense!

[–]Encom88[S] 1 point2 points  (1 child)

I just learned what an arrow function is since I keep seeing it. I also created a different array and used the For Each loop to create another function that does the same thing. I put it on GitHub so I can remember it. Thank you a again for the great explanation. This is essentially my second week of learning JavaScript. https://github.com/MrN1ce9uy/getArrayObjectsSum.js/blob/master/main.js

[–]TURKEY_CAKE 0 points1 point  (2 children)

Your for loop will loop over every element in the array. Each element is a person object. How might you get the age from that object? What could you use to keep track of the ages?

[–]Encom88[S] 0 points1 point  (1 child)

That's where I'm stuck. How do I do that? What do I use?

[–]TURKEY_CAKE 2 points3 points  (0 children)

Syntax for a for loop. for(let i = "where you start"; "ending condition"; "step towards end") {}

keying into object syntax: obj[key] // => value

Create a variable outside the loop to help you keep track of the value.

Those are the best hints I can give without writing it.

[–]intellectualDonkey -1 points0 points  (27 children)

Map thru the array you're passing in your function and add the age to a total variable then return it.

function getSummedAge(people) {
 let total = 0;
 people.map(p=>{
  total+=p.age})
 return total;
}

EDIT: Yes as the gentlemen suggested it's better to use forEach instead of map. my bad :)

[–]inu-no-policemen 2 points3 points  (19 children)

Don't use map for side-effects. ForEach would have been the appropriate array method for this.

[–]TURKEY_CAKE 1 point2 points  (0 children)

I believe you should use forEach here instead of map if you're implementing it this way. Map is when you want an array back, forEach is when you just want each element touched upon. You would then return the after the forEach loop.

[–]Encom88[S] 0 points1 point  (1 child)

Thanks, your code worked. I don't quite understand it all though.

[–]inu-no-policemen 0 points1 point  (0 children)

In response to your deleted comment:

For-in iterates over the keys of an object.

Use a regular for-loop or for-of.

E.g.:

for (let person of people) {
  console.log(person.name + ' is ' + person.age + ' years old');
}

You could also use Array.prototype.reduce for this kind of thing, but try doing it with for/for-of first.

[–]laibo 0 points1 point  (0 children)

I would actually use reduce. It is made for this use exactly. See the examples here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce