forked from BushraAlabsi/Toy_Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaverageAge.js
27 lines (23 loc) · 945 Bytes
/
averageAge.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// 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) {
// YOUR CODE HERE
var ave=0;
var len = 0
for (var i = 0; i < people.length; i++) {
if(people[i].age>18 && people[i].age <50){
ave = ave + people[i].age;
len ++;
}
}
return ave/len
}
averageAge(people); // 43+36+44+24 = 36.74