Created
August 5, 2018 23:09
-
-
Save Odame/c3cc0905a8b7d4e2255f19a8f8dd7923 to your computer and use it in GitHub Desktop.
Solution to test question in form.
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
/** | |
* 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'); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment