Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solved lab #489

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions src/presidents.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,47 +419,74 @@ const presidents = [


// Iteration 1 | Names of All Presidents - `map()`
function getNames(presidentsArr) {}
function getNames(presidentsArr) {
return presidentsArr.map((president) => president.name);
}




// Iteration 2 | Democratic Presidents - `filter()`
function getDemocraticPresidents(presidentsArr) {}
function getDemocraticPresidents(presidentsArr) {
return presidentsArr.filter((president) => president.party === "Democratic");
}




// Iteration 3 | Count Years in Office - reduce()
function countYearsInOffice(presidentsArr) {}
/*Important: You should skip the president who is still in office (the president with the leftOffice property set to null) and not include them in the total years.*/
function countYearsInOffice(presidentsArr) {
return presidentsArr.reduce((totalYears, president) => {
if (president.leftOffice) {
totalYears += president.leftOffice - president.tookOffice;
}
return totalYears;
}, 0);
}




// Iteration 4 | Sort Presidents by Birth Year - `sort()`
function sortPresidentsByBirthYear(presidentsArr) {}
function sortPresidentsByBirthYear(presidentsArr) {
return presidentsArr.sort((a, b) => a.birthYear - b.birthYear);
}




// Bonus: Iteration 5 | Age at Inauguration - `map()`
function getAgeAtInauguration(presidentsArr) {}
function getAgeAtInauguration(presidentsArr) {
return presidentsArr.map((president) => {
return {
name: president.name,
ageAtInauguration: president.tookOffice - president.birthYear
};
});
}




// Bonus: Iteration 6 | Presidents Born After - `filter()`
function getPresidentsBornAfter(presidentsArr, year) {}
function getPresidentsBornAfter(presidentsArr, year) {
return presidentsArr.filter((president) => president.birthYear > year);
}




// Bonus: Iteration 7 | Count Republican Presidents
function countRepublicanPresidents(presidentsArr) {}
function countRepublicanPresidents(presidentsArr) {
return presidentsArr.filter((president) => president.party === "Republican").length;
}




// Bonus: Iteration 8 | Sort Presidents by Name - `sort()`
function sortPresidentsByName(presidentsArr) {}
function sortPresidentsByName(presidentsArr) {
return presidentsArr.sort((a, b) => a.name.localeCompare(b.name));
}