From 1accae58513d9c6ef2dc6eff7b679c931d2b9bc9 Mon Sep 17 00:00:00 2001 From: matt Date: Mon, 18 Apr 2022 21:38:16 +0000 Subject: [PATCH] Added GradeBoundaries class --- lib/GradeBoundaries.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 lib/GradeBoundaries.js diff --git a/lib/GradeBoundaries.js b/lib/GradeBoundaries.js new file mode 100644 index 0000000..a8744ee --- /dev/null +++ b/lib/GradeBoundaries.js @@ -0,0 +1,35 @@ +'use strict'; + +class GradeBoundaries { + boundaries; + + constructor(boundaries = { + 'A*': 90, + 'A': 80, + 'B': 70, + 'C': 60, + 'D': 50, + 'E': 40, + 'F': 0 + }) { + this.boundaries = boundaries; + } + + getGrade(score) { + const boundaries = Object.values(this.boundaries); + + for (const [ i, number ] of boundaries.entries()) { + if (score >= number) + return Object.entries(this.boundaries)[i][0]; + } + } + + toString() { + return Object.entries(this.boundaries).reduce((res, b) => { + return `${res}\n${b[0]}${' '.repeat(2 - b[0].length)}` + + `: >= ${b[1]}`; + }, ''); + } +} + +module.exports = GradeBoundaries;