Created
November 4, 2019 20:52
-
-
Save samkcarlile/81a5ba0f4e09ba5d68fd4eb174ebe75a to your computer and use it in GitHub Desktop.
Cool letter grade to point value conversion
This file contains 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
/** | |
* | |
* @param {string} letter the string representation of the grade. Examples: ["a-", "A", "F", "b+"] | |
* @param {number} points the number of points that an perfect A represents on the scale | |
* @param {number} inc the number of points in between each full letter grade | |
*/ | |
function grade(letter = "", points = 4, inc = 1) { | |
if (letter.length < 1 || letter.length > 2) { | |
throw 'the length of the letter grade string must be equal to 1 or 2' | |
} | |
letter = letter.toUpperCase() | |
let lettercode = letter.charCodeAt(0) | |
let value = points - ((lettercode - 65) * inc) | |
value = value < 0 ? 0 : value | |
if (letter.length < 2) return value | |
let modcode = letter.charCodeAt(1) | |
let mod = 0.333 * (modcode == 43 ? 1 : -1) | |
return value + mod | |
} | |
module.exports = grade |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment