Last active
September 11, 2018 17:20
-
-
Save krg7880/1729f765d794fbf293009213708a0bb5 to your computer and use it in GitHub Desktop.
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
| const scores = [ | |
| { id: 1, newScore: 'A+', oldScore: 'F-' }, | |
| { id: 2, newScore: 'B-', oldScore: 'D+' }, | |
| { id: 3, newScore: 'A', oldScore: 'A-' }, | |
| { id: 4, newScore: 'B', oldScore: 'F' }, | |
| { id: 5, newScore: 'A-', oldScore: 'A' }, | |
| { id: 6, newScore: 'A', oldScore: 'Z' }, | |
| { id: 7, newScore: 'A-', oldScore: 'B+' }, | |
| { id: 7, newScore: 'A-', oldScore: 'F-' }, | |
| { id: 7, newScore: 'A-', oldScore: 'B-' } | |
| ] | |
| const plusMinusScore = b => { | |
| if (b) { | |
| if (b === '-') { | |
| return -0.1 | |
| } | |
| return 0.1 | |
| } | |
| return 0 | |
| } | |
| /** | |
| * Sum the grade including the plus and minus. | |
| * Normalize plus and minus to 1 and 2 respectively | |
| * | |
| * @param {Array} score of the letter grade (inluding plus/minus) | |
| * @param {string} score.a Letter grade | |
| * @param {string} socre.b Plus|Minus with grade, if any | |
| * @return {number} | |
| */ | |
| const sumScore = ([a, b]) => { | |
| const plusMinus = plusMinusScore(b) | |
| return b ? a.charCodeAt() + plusMinus : a.charCodeAt() | |
| } | |
| /** | |
| * | |
| * @param {Object} record Represents a single object in the array | |
| * @param {string} record.newScore Destructure the `newScore` property of the record | |
| * @param {string} record.oldScore Destructure the `oldScore` property of the record | |
| * @return {number} Returns the delta in percentage | |
| */ | |
| const computeDelta = ({ newScore, oldScore }) => { | |
| const newScoreSum = sumScore(newScore.split('')) | |
| const oldScoreSum = sumScore(oldScore.split('')) | |
| return newScoreSum > oldScoreSum | |
| ? newScoreSum % oldScoreSum | |
| : oldScoreSum % newScoreSum | |
| } | |
| /** | |
| * Iterates the collection of scores, applies a delta to each | |
| * record and sort by the greatest difference between the `oldScore` | |
| * and `newScore` | |
| * | |
| * @param {Array} scores | |
| * @return {Array} | |
| */ | |
| const compute = scores => { | |
| return scores.map(score => { | |
| const delta = computeDelta(score) | |
| return { ...score, delta } | |
| }) | |
| } | |
| /** | |
| * Sorts an array of objects based on the delta | |
| * | |
| * @param {Object} a | |
| * @param {Object} b | |
| */ | |
| const comparator = (a, b) => { | |
| if (a.delta > b.delta) return -1 | |
| if (a.delta < b.delta) return 1 | |
| return 0 | |
| } | |
| const topK = (k, collection) => collection.slice(0, k) | |
| const newScores = compute(scores).sort(comparator) | |
| console.log('\r\n ------- ALL SCORES --------- \r\n') | |
| console.log(newScores) | |
| console.log('\r\n ------- TOP 4 SCORES --------- \r\n') | |
| console.log(topK(4, newScores)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment