Skip to content

Instantly share code, notes, and snippets.

@prmichaelsen
Last active July 4, 2022 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prmichaelsen/a5140e404e239e3ca2fc0c04da791812 to your computer and use it in GitHub Desktop.
Save prmichaelsen/a5140e404e239e3ca2fc0c04da791812 to your computer and use it in GitHub Desktop.
formats letter grades using [threshold, character] pairs
#!/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