-
Notifications
You must be signed in to change notification settings - Fork 35
/
averageAge.js
21 lines (18 loc) · 885 Bytes
/
averageAge.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Write a function called averageAge that accpets an array of objects and return the average ages for the people who are between 18 and 50
var people = [
{name: {first: 'Ahmad', middle: 'B.', last: 'AlAhmad'}, age: 85},
{name: {first: 'Amer', last: 'MHD'}, age: 43},
{name: {first: 'Aya', last: 'Sultan'}, age: 36},
{name: {first: 'Hadeel', middle: 'E.', last: 'Lami'}, age: 44},
{name: {first: 'Hadeel', middle: 'E.', last: 'Lami'}, age: 54},
{name: {first: 'Lina', last: 'MHD'}, age: 14} ,
{name: {first: 'Obada', last: 'Eddin'}, age: 24}
];
function averageAge(people) {
var count =0, sum=0;
for (var i = 0; i < people.length; i++) {
if(people[i].age >=18 && people[i].age<=50){sum = sum +people[i].age; count++}
}
return sum/count;
}
averageAge(people); // 43+36+44+24 = 36.74