Files
getGrade-AppsScript/main.mjs
2024-08-26 16:28:26 +05:30

62 lines
1.1 KiB
JavaScript

import { argv } from 'node:process';
import { env } from 'node:process';
const DEBUG = Boolean(env.DEBUG);
const grades = [
{
grade: 'A1', min: 91, max: 100
},
{
grade: 'A2', min: 81, max: 90
},
{
grade: 'B1', min: 71, max: 80
},
{
grade: 'B2', min: 61, max: 70
},
{
grade: 'C1', min: 51, max: 60
},
{
grade: 'C2', min: 41, max: 50
},
{
grade: 'D', min: 33, max: 40
},
{
grade: 'E', min: 0, max: 32
}
]
/**
* Returns the grade corresponding to mark.
*
* @param {number} input The marks out of 100.
* @return The corresponding grade of the marks.
* @customfunction
*/
function getGrade(mark){
if (isNaN(Number(mark))) return 'Ab'
if (mark > 100 || mark < 0) return 'OUT OF MARK RANGE'
main: for (let i=0;i<grades.length;i++){
printdebug({i})
if (mark > grades[i].max) continue
while (grades[i].min > mark){
if (++i === grades.length) break main
printdebug({i})
}
return grades[i].grade
}
}
const grade = getGrade(argv[2])
console.log(grade)
function printdebug(text){ if (DEBUG)console.error('DEBUG:',text) }