Skip to content

Instantly share code, notes, and snippets.

@samuelayo samuelayo/test.js
Created Aug 14, 2018

Embed
What would you like to do?
const list = [
{ name: 'test a', ranking: 3 },
{ name: 'test b', ranking: 7 },
{ name: 'test c', ranking: 8 },
{ name: 'test d', ranking: 2 },
{ name: 'test e', ranking: 5 },
{ name: 'test f', ranking: 2 },
{ name: 'test g', ranking: 6 },
{ name: 'test h', ranking: 7 },
{ name: 'test i', ranking: 9 },
{ name: 'test j', ranking: 8 },
{ name: 'test k', ranking: 4 },
{ name: 'test l', ranking: 1 },
{ name: 'test m', ranking: 10 },
]
const sortListByRanking = (list) => {
return list.sort((a, b) => a.ranking - b.ranking);
}
const getAverageRankingFromList = (list) => {
return list.reduce((a, b) => a + b.ranking, 0) / list.length
}
console.log(sortListByRanking(list));
// expected response
// [ { name: 'test l', ranking: 1 },
// { name: 'test d', ranking: 2 },
// { name: 'test f', ranking: 2 },
// { name: 'test a', ranking: 3 },
// { name: 'test k', ranking: 4 },
// { name: 'test e', ranking: 5 },
// { name: 'test g', ranking: 6 },
// { name: 'test h', ranking: 7 },
// { name: 'test b', ranking: 7 },
// { name: 'test j', ranking: 8 },
// { name: 'test c', ranking: 8 },
// { name: 'test i', ranking: 9 },
// { name: 'test m', ranking: 10 } ]
console.log(getAverageRankingFromList(list));
// expected response
// 5.538461538461538
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.