Skip to content

Instantly share code, notes, and snippets.

@codebubb
Last active January 10, 2024 12:56
Show Gist options
  • Save codebubb/2c2b247c5d7de6df03d87f9458c4ecc0 to your computer and use it in GitHub Desktop.
Save codebubb/2c2b247c5d7de6df03d87f9458c4ecc0 to your computer and use it in GitHub Desktop.
Array of People
const people = [
{ firstName: 'Sam', lastName: 'Hughes', DOB: '07/07/1978', department: 'Development', salary: '45000' },
{ firstName: 'Terri', lastName: 'Bishop', DOB: '02/04/1989', department: 'Development', salary: '35000' },
{ firstName: 'Jar', lastName: 'Burke', DOB: '11/01/1985', department: 'Marketing', salary: '38000' },
{ firstName: 'Julio', lastName: 'Miller', DOB: '12/07/1991', department: 'Sales', salary: '40000' },
{ firstName: 'Chester', lastName: 'Flores', DOB: '03/15/1988', department: 'Development', salary: '41000' },
{ firstName: 'Madison', lastName: 'Marshall', DOB: '09/22/1980', department: 'Sales', salary: '32000' },
{ firstName: 'Ava', lastName: 'Pena', DOB: '11/02/1986', department: 'Development', salary: '38000' },
{ firstName: 'Gabriella', lastName: 'Steward', DOB: '08/26/1994', department: 'Marketing', salary: '46000' },
{ firstName: 'Charles', lastName: 'Campbell', DOB: '09/04/1977', department: 'Sales', salary: '42000' },
{ firstName: 'Tiffany', lastName: 'Lambert', DOB: '05/11/1990', department: 'Development', salary: '34000' },
{ firstName: 'Antonio', lastName: 'Gonzalez', DOB: '03/24/1985', department: 'Office Management', salary: '49000' },
{ firstName: 'Aaron', lastName: 'Garrett', DOB: '09/04/1985', department: 'Development', salary: '39000' },
];
// Exercises
// 1) What is the average income of all the people in the array?
// 2) Who are the people that are currently older than 30?
// 3) Get a list of the people's full name (firstName and lastName).
// 4) Get a list of people in the array ordered from youngest to oldest.
// 5) How many people are there in each department?
@rnura1234
Copy link

// 1) What is the average income of all the people in the array?
const averageIncome=(arr)=>{
let totalIncomeOfAlltheEmployee=0;
let totalEmployee=arr.length
for(let i=0;i<arr.length;i++){
totalIncomeOfAlltheEmployee+=arr[i].salary*1;
}
console.log(totalIncomeOfAlltheEmployee)
return totalIncomeOfAlltheEmployee/totalEmployee

}

console.log(averageIncome(people))

// 2) Who are the people that are currently older than 30?
const oldThan30=(arr)=>{
const d=new Date();
const yy=d.getFullYear();
return arr.filter(item=> yy-item.DOB.split('/')[2]>30)
}
console.log(oldThan30(people))

// 3) Get a list of the people's full name (firstName and lastName).

const peopleFullName=people.map(item=>${item.firstName} ${item.lastName})
console.log(peopleFullName)

// 4) Get a list of people in the array ordered from youngest to oldest.

const sortPeopleByAge=(arr)=>{
return arr.sort((a,b)=>new Date(a.DOB)-new Date(b.DOB))
}
console.log(sortPeopleByAge(people))

// 5) How many people are there in each department?

const numberOfPeaopeInDepartment=(people)=>{
let obj={}
for(let i=0;i<people.length;i++){
if(obj[people[i].department]){
obj[people[i].department]+=1
}else{
obj[people[i].department]=1
}
}
return obj
}
console.log(numberOfPeaopeInDepartment(people))

@anshdeshwal
Copy link

for question 2
const olderThirty = (p = people) => {
const currentDate = new Date();
const thirtyYearsAgo = currentDate.getFullYear() - 30;

let a = p.filter((person) => {
    const dob = new Date(person.DOB);
    const dobYear = dob.getFullYear();
    const dobMonth = dob.getMonth() + 1;
    const dobDay = dob.getDate();
    return (dobYear < thirtyYearsAgo) || (dobYear === thirtyYearsAgo && dobMonth < currentDate.getMonth() + 1) || (dobYear === thirtyYearsAgo && dobMonth === currentDate.getMonth() + 1 && dobDay <= currentDate.getDate());
});
return a;

};
console.log(olderThirty());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment