Skip to content

Instantly share code, notes, and snippets.

@dresende
Created June 22, 2017 16:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dresende/fe30666c3f1c7a3e7c09a45bc29b567c to your computer and use it in GitHub Desktop.
Save dresende/fe30666c3f1c7a3e7c09a45bc29b567c to your computer and use it in GitHub Desktop.
const people = [
{ name: "John", ranking: 3 },
{ name: "Jane", ranking: 8 },
{ name: "Joe", ranking: 2 },
{ name: "Jessica", ranking: 9 },
];
console.log("average", rank_avg(people));
console.log("sorted", rank_sort(people).map((person) => (person.name)));
function rank_sort(people) {
// ascending
return people.sort((person1, person2) => {
return person1.ranking - person2.ranking;
})
}
function rank_avg(people) {
// ascending
return people.reduce((rank, person) => (rank + person.ranking), 0) / people.length;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment