Skip to content

Instantly share code, notes, and snippets.

@osmartinez
Created July 30, 2019 14:44
Show Gist options
  • Save osmartinez/7ed7e90595514d5b80f2e4a7ad85cc94 to your computer and use it in GitHub Desktop.
Save osmartinez/7ed7e90595514d5b80f2e4a7ad85cc94 to your computer and use it in GitHub Desktop.
Answer question 7 - Oscar Martinez
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