Created
June 22, 2017 16:30
-
-
Save dresende/fe30666c3f1c7a3e7c09a45bc29b567c to your computer and use it in GitHub Desktop.
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
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