Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // The inupt array is array of objects, each object is in the following format:
- // {
- // name: <String>,
- // rank: <Number>,
- // }
- //
- /**
- * Returns the avarage ranking of all the input
- */
- const average = (arr) => {
- let sum = 0;
- arr.forEach((element) => {
- sum += element.rank;
- });
- return sum / arr.length;
- }
- // OR ES6 average
- const es6Average = arr => arr.map(a => a.rank).reduce((a, b) => a + b) / arr.length;
- /**
- * Return a sorted array based on ranking.
- *
- * Note: this function mutates the original array, and it's not creating a new result array.
- */
- const sortArray = (arr) => {
- return arr.sort((a, b) => { return a.rank - b.rank });
- }
Add Comment
Please, Sign In to add comment