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 with bonuses! #470

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
66 changes: 43 additions & 23 deletions src/presidents.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,47 +419,67 @@ const presidents = [


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


function getNames(presidentsArr) {
const presidentNames = presidentsArr.map(president => president.name);
return presidentNames;
}


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


function getDemocraticPresidents(presidentsArr) {
const filteredDemocraticPresidents = presidentsArr.filter(president => president.party === "Democratic");
return filteredDemocraticPresidents;
}


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


function countYearsInOffice(presidentsArr) {
const countYears = presidentsArr.reduce((aux, president) => {
if (president.leftOffice !== null) {
return aux + (president.leftOffice - president.tookOffice);
} else {
return aux;
}
}, 0);
return countYears;
}


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


function sortPresidentsByBirthYear(presidentsArr) {
return presidentsArr.sort((presidentA, presidentB) => {
return presidentA.birthYear - presidentB.birthYear;
});
}


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


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


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



function getPresidentsBornAfter(presidentsArr, year) {
const presidentsBornAfter = presidentsArr.filter(president => president.birthYear > year);
return presidentsBornAfter;
}

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


function countRepublicanPresidents(presidentsArr) {
const republicanPresidents = presidentsArr.reduce((aux, president) => {
if (president.party === "Republican") aux++;
return aux;
}, 0);
return republicanPresidents;
}


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

2 changes: 1 addition & 1 deletion tests/bonus.specs.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
xdescribe("BONUS", () => {
describe("BONUS", () => {
// Bonus: Iteration 5
describe("Bonus: Iteration 5 | Age at Inauguration", () => {
it("should take 1 argument (presidents)", () => {
Expand Down