Guest User

Untitled

a guest
Aug 15th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. //
  2. // The inupt array is array of objects, each object is in the following format:
  3. // {
  4. // name: <String>,
  5. // rank: <Number>,
  6. // }
  7. //
  8.  
  9. /**
  10. * Returns the avarage ranking of all the input
  11. */
  12. const average = (arr) => {
  13. let sum = 0;
  14. arr.forEach((element) => {
  15. sum += element.rank;
  16. });
  17.  
  18. return sum / arr.length;
  19. }
  20.  
  21. // OR ES6 average
  22. const es6Average = arr => arr.map(a => a.rank).reduce((a, b) => a + b) / arr.length;
  23.  
  24. /**
  25. * Return a sorted array based on ranking.
  26. *
  27. * Note: this function mutates the original array, and it's not creating a new result array.
  28. */
  29. const sortArray = (arr) => {
  30. return arr.sort((a, b) => { return a.rank - b.rank });
  31. }
Add Comment
Please, Sign In to add comment