Last active
July 4, 2022 07:37
-
-
Save prmichaelsen/a5140e404e239e3ca2fc0c04da791812 to your computer and use it in GitHub Desktop.
formats letter grades using [threshold, character] pairs
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
#!/usr/bin/env ts-node | |
const thresholds = [ | |
90, | |
80, | |
70, | |
60, | |
0, | |
]; | |
const letters = [ | |
'A', | |
'B', | |
'C', | |
'D', | |
'F', | |
]; | |
const signThresholds = [ | |
6, | |
3, | |
0, | |
]; | |
const signs = [ | |
'+', | |
'', | |
'-', | |
]; | |
const formatLetterGrade = (val: number) => { | |
const letter = | |
letters[thresholds.findIndex((threshold) => val >= threshold)]; | |
let sign | |
= signs[val === 100 ? 0 : signThresholds.findIndex(threshold => val % 10 >= threshold)]; | |
return letter + sign; | |
} | |
console.log('A+', formatLetterGrade(100)); | |
console.log('A+', formatLetterGrade(96)); | |
console.log( 'B+', formatLetterGrade(89)); | |
console.log( 'C-', formatLetterGrade(70)); | |
console.log( 'C ', formatLetterGrade(73)); | |
console.log( 'C ', formatLetterGrade(75)); | |
console.log( 'D+', formatLetterGrade(69)); | |
console.log( 'F-', formatLetterGrade(50)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment