Created
August 5, 2018 23:09
-
-
Save Odame/c3cc0905a8b7d4e2255f19a8f8dd7923 to your computer and use it in GitHub Desktop.
Revisions
-
Odame created this gist
Aug 5, 2018 .There are no files selected for viewing
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 charactersOriginal 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'); };