Created
June 4, 2019 08:52
-
-
Save samundrak/6931e8b694b330cba3c85364edd0078b to your computer and use it in GitHub Desktop.
This file contains 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
const collection = [ | |
{ name: 'samundra', rank: 3 }, | |
{ name: 'abina', rank: 5 }, | |
{ name: 'hari', rank: 1 }, | |
{ name: 'bibek', rank: 2 }, | |
{ name: 'suman', rank: 4 }, | |
]; | |
function sortBy(collection, compareBy) { | |
return collection.sort((item, lastItem) => { | |
return item[compareBy] > lastItem[compareBy]; | |
}); | |
} | |
function getAverage(collection, averageBy) { | |
return sortBy(collection, averageBy).map(item => item[averageBy])[ | |
Math.floor(collection.length / 2) | |
]; | |
} | |
console.log(sortBy(collection, 'rank')); | |
console.log(getAverage(collection, 'rank')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment