forked from BushraAlabsi/Toy_Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaverageAge.js
More file actions
26 lines (20 loc) · 903 Bytes
/
Copy pathaverageAge.js
File metadata and controls
26 lines (20 loc) · 903 Bytes
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
// 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 newarray=[];
for(var i=0; i<people.length ;i++){
if ( people[i].age>18 && people[i].age<=50){
return newarray.push(people[i].age)
}
}
return newarray[i]/newarray.length
}
averageAge(people); // 43+36+44+24 = 36.74