Created
June 14, 2018 01:39
-
-
Save ChuckJonas/5a91ded8d39b72bf3aee17efedc857d9 to your computer and use it in GitHub Desktop.
Functional Method Array Challenge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//=== CHALLENGE #1 === | |
type Data1Type = { | |
name: string, | |
timeEntries: { credit: boolean, hours: number }[], | |
role: string | |
} | |
type Solution1Type = { name: string, totalHours: number } | |
let data1: Data1Type[] = [ | |
{ | |
name: 'John Doe', | |
timeEntries: [ | |
{ credit: false, hours: 1 }, | |
{ credit: false, hours: 2 }, | |
{ credit: false, hours: 3 }, | |
{ credit: true, hours: 1 }, | |
], | |
role: 'developer' | |
}, | |
{ | |
name: 'jimmy john', | |
timeEntries: [ | |
{ credit: false, hours: 1 }, | |
{ credit: true, hours: 1 }, | |
], | |
role: 'dm' | |
}, | |
{ | |
name: 'jane doe', | |
timeEntries: [ | |
{ credit: false, hours: 5 }, | |
{ credit: true, hours: 1 }, | |
], | |
role: 'developer' | |
}, | |
{ | |
name: 'Bo Jangels', | |
timeEntries: [ | |
{ credit: false, hours: 5 }, | |
{ credit: true, hours: 1 }, | |
{ credit: false, hours: 10 } | |
], | |
role: 'developer' | |
} | |
]; | |
// DO WORK HERE!!! | |
// sum the total non-credited hours to all developer. | |
// create an array with name & totalHours | |
// sort by totalHours desc | |
let solution1: Solution1Type[] = data1 | |
.filter(person => person.role === 'developer') | |
.map(dev => { | |
let total = dev.timeEntries.filter(entry => !entry.credit) | |
.reduce((total, entry) => total + entry.hours, 0); | |
return { | |
name: dev.name, | |
totalHours: total | |
} | |
}).sort((a, b) => b.totalHours - a.totalHours); | |
console.log(JSON.stringify(solution1)); | |
let answer1 = '[{"name":"Bo Jangels","totalHours":15},{"name":"John Doe","totalHours":6},{"name":"jane doe","totalHours":5}]' | |
if (JSON.stringify(solution1) !== answer1) { | |
alert('failed #1!'); | |
} else { | |
alert('passed #1!'); | |
} | |
//=== CHALLENGE #2 === | |
type Data2Type = { | |
clientName: string, | |
projects: { name: string, budget: number }[] | |
} | |
type Solution2Type = Data2Type & { totalBudget: number } | |
let data2: Data2Type[] = [ | |
{ | |
clientName: 'acme', | |
projects: [ | |
{ | |
name: 'rocket shoes', | |
budget: 50 | |
} | |
] | |
}, | |
{ | |
clientName: 'prestige worldwide', | |
projects: [ | |
{ | |
name: 'activities tracker', | |
budget: 25 | |
}, | |
{ | |
name: 'asdf', | |
budget: 15 | |
}, | |
] | |
}, | |
{ | |
clientName: 'acme', | |
projects: [ | |
{ | |
name: 'pie machine', | |
budget: 25 | |
}, | |
{ | |
name: 'road runner trap', | |
budget: 15 | |
}, | |
] | |
} | |
] | |
// DO WORK HERE!!! | |
// de-duplicate the client data | |
// order projects by budget desc | |
// add new field "totalBudget" -> sum of project budgets | |
// order by client name asc | |
let solution2: Solution2Type[] = data2.reduce( | |
(clients, client) => { | |
let item = clients.find(item => item.clientName === client.clientName); | |
if (item) { | |
item.projects = item.projects.concat(client.projects); | |
} else { | |
clients.push(client); | |
} | |
return clients; | |
}, | |
new Array<Data2Type>() | |
).map(client => { | |
return { | |
...client, | |
projects: client.projects.sort((a, b) => b.budget - a.budget), | |
totalBudget: client.projects.reduce((total, proj) => total + proj.budget, 0) | |
} | |
}) | |
console.log(JSON.stringify(solution2)); | |
let answer2 = '[{"clientName":"acme","projects":[{"name":"rocket shoes","budget":50},{"name":"pie machine","budget":25},{"name":"road runner trap","budget":15}],"totalBudget":90},{"clientName":"prestige worldwide","projects":[{"name":"activities tracker","budget":25},{"name":"asdf","budget":15}],"totalBudget":40}]'; | |
if (JSON.stringify(solution2) !== answer2) { | |
alert('failed #2!'); | |
} else { | |
alert('passed #2!'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment