Skip to content

Instantly share code, notes, and snippets.

@Odame
Created August 5, 2018 23:09
Show Gist options
  • Save Odame/c3cc0905a8b7d4e2255f19a8f8dd7923 to your computer and use it in GitHub Desktop.
Save Odame/c3cc0905a8b7d4e2255f19a8f8dd7923 to your computer and use it in GitHub Desktop.

Revisions

  1. Odame created this gist Aug 5, 2018.
    22 changes: 22 additions & 0 deletions ranking.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    /**
    * Sort an array of objects based on ranking
    * @param {{name: String, ranking: Number}[]} objectArray
    */
    const sortByRanking = (objectArray) => {
    return objectArray.sort((obj1, obj2) => obj1.ranking-obj2.ranking);
    };


    /**
    * Find the average ranking of an array of objects.
    * Throws an exception if objectArray is empty
    * @param {{name: String, ranking: Number}[]} objectArray
    */
    const computeAverageRanking = (objectArray) => {
    if (objectArray.length) {
    const sumOfRanking = objectArray.reduce((prev, obj) => prev + obj.ranking, 0);
    const numOfObjects = objectArray.length;
    return sumOfRanking/numOfObjects;
    }
    throw new Error('objectArray param, must be non-empty');
    };