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 #486

Open
wants to merge 2 commits 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
72 changes: 40 additions & 32 deletions src/presidents.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,54 +412,62 @@ const presidents = [
tookOffice: 2021,
leftOffice: null,
party: "Democratic",
}
},
];




// Iteration 1 | Names of All Presidents - `map()`
function getNames(presidentsArr) {}



function getNames(presidentsArr) {
return presidentsArr.map((presidents) => presidents.name);
}
console.log(getNames(presidents));

// Iteration 2 | Democratic Presidents - `filter()`
function getDemocraticPresidents(presidentsArr) {}



function getDemocraticPresidents(presidentsArr) {
return presidentsArr.filter(
(presidents) => presidents.party === "Democratic"
);
}
console.log(getDemocraticPresidents(presidents));

// Iteration 3 | Count Years in Office - reduce()
function countYearsInOffice(presidentsArr) {}



function countYearsInOffice(presidentsArr) {
return presidentsArr.reduce(function (totalYears, president) {
if (president.leftOffice === "null ") return totalYears;
const yearsInOffice = president.leftOffice - president.tookOffice;
return totalYears + yearsInOffice;
}, 0);
}
console.log(countYearsInOffice(presidents));

// Iteration 4 | Sort Presidents by Birth Year - `sort()`
function sortPresidentsByBirthYear(presidentsArr) {}



function sortPresidentsByBirthYear(presidentsArr) {
return presidentsArr.sort((a, b) => a.birthYear - b.birthYear);
}
console.log(sortPresidentsByBirthYear(presidents));

// Bonus: Iteration 5 | Age at Inauguration - `map()`
function getAgeAtInauguration(presidentsArr) {}



function getAgeAtInauguration(presidentsArr) {
const arrInauguration = presidentsArr.map((president) => {
const ageOfInauguration = president.tookOffice - president.birthYear;
return {
...president,
ageOfInauguration: ageOfInauguration,
};
});
return arrInauguration;
}
console.log(getAgeAtInauguration(presidents));

// Bonus: Iteration 6 | Presidents Born After - `filter()`
function getPresidentsBornAfter(presidentsArr, year) {}



function getPresidentsBornAfter(presidentsArr, year) {
return presidentsArr.filter((president) => {
president.birthYear > year;
});
}
console.log(getPresidentsBornAfter(presidents,1790));

// Bonus: Iteration 7 | Count Republican Presidents
function countRepublicanPresidents(presidentsArr) {}




// Bonus: Iteration 8 | Sort Presidents by Name - `sort()`
function sortPresidentsByName(presidentsArr) {}