-
-
Save osmartinez/7ed7e90595514d5b80f2e4a7ad85cc94 to your computer and use it in GitHub Desktop.
Answer question 7 - Oscar Martinez
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
scores = [ | |
{ | |
'ranking': 7.4, | |
'name': 'Liza', | |
}, | |
{ | |
'ranking': 3.2, | |
'name': 'Bob', | |
}, | |
{ | |
'ranking': 7.8, | |
'name': 'Oscar', | |
}, | |
] | |
const sortAscending = (items, prop) => items.sort((a,b) => (a[prop] > b[prop]) ? 1 : ((b[prop] > a[prop]) ? -1 : 0)) | |
const calculateAvg = (items, prop) => items.reduce((a, b) => a + b[prop], 0) / items.length; | |
console.log(sortAscending(scores,'ranking')) | |
console.log(calculateAvg(scores, 'ranking')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment