Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
2019-Sep-16 Dev.to Challenge (Grade Book)
const sum = (...nums) => nums.reduce((total, cur) => total + cur, 0);
const meanGrade = (...grades) => sum(...grades) / grades.length;
const letterForGrade = grade => {
const lastDigit = grade % 10;
const gradeLetter = Object.entries({90: "A", 80: "B", 70: "C", 60: "D", 0: "F"})
.sort(([a], [b]) => a < b)
.find(([minGrade]) => grade >= minGrade)
[1] + ((lastDigit < 5) ? "-" :
(lastDigit > 5) ? "+" : "");
return gradeLetter;
};
//---
const assert = (fn, expected) => {
const r = fn();
if (r !== expected) {
throw new Error(`$Expected ${expected}, saw ${r}`);
}
}
assert(() => (letterForGrade(meanGrade(64, 55, 92))), "C-");
assert(() => (letterForGrade(meanGrade(99, 89, 93))), "A-");
assert(() => (letterForGrade(meanGrade(33, 99, 95))), "C+");
assert(() => (letterForGrade(meanGrade(95, 95, 95))), "A");
assert(() => (letterForGrade(meanGrade(100, 100, 99 ))), "A+");
assert(() => (letterForGrade(meanGrade(0, 0, 0 ))), "F-");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.