student-academic-toolkit 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -2
- package/index.js +170 -0
- package/package.json +15 -4
- package/test.js +50 -0
package/README.md
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
-
#
|
|
1
|
+
# student-academic-toolkit
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Academic helper functions for Node.js: CGPA, attendance, 75% eligibility, percentage, grades, and study time.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install student-academic-toolkit
|
package/index.js
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
function assertFiniteNumber(value, label) {
|
|
2
|
+
if (typeof value !== 'number' || Number.isNaN(value) || !Number.isFinite(value)) {
|
|
3
|
+
throw new TypeError(`${label} must be a finite number`);
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function roundTo(num, decimals = 2) {
|
|
8
|
+
const factor = 10 ** decimals;
|
|
9
|
+
return Math.round(num * factor) / factor;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// 1. CGPA Calculator: subjects = [{ gradePoints, credits }]
|
|
13
|
+
function calculateCGPA(subjects, maxGradePoint = 10) {
|
|
14
|
+
if (!Array.isArray(subjects) || subjects.length === 0) {
|
|
15
|
+
throw new Error('subjects must be a non-empty array');
|
|
16
|
+
}
|
|
17
|
+
assertFiniteNumber(maxGradePoint, 'maxGradePoint');
|
|
18
|
+
if (maxGradePoint <= 0) {
|
|
19
|
+
throw new Error('maxGradePoint must be greater than 0');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
let totalWeightedPoints = 0;
|
|
23
|
+
let totalCredits = 0;
|
|
24
|
+
|
|
25
|
+
for (const item of subjects) {
|
|
26
|
+
if (!item || typeof item !== 'object') {
|
|
27
|
+
throw new TypeError('Each subject entry must be an object');
|
|
28
|
+
}
|
|
29
|
+
assertFiniteNumber(item.gradePoints, 'gradePoints');
|
|
30
|
+
assertFiniteNumber(item.credits, 'credits');
|
|
31
|
+
if (item.credits <= 0) {
|
|
32
|
+
throw new Error('credits must be greater than 0');
|
|
33
|
+
}
|
|
34
|
+
if (item.gradePoints < 0 || item.gradePoints > maxGradePoint) {
|
|
35
|
+
throw new RangeError(`gradePoints must be between 0 and ${maxGradePoint}`);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
totalWeightedPoints += item.gradePoints * item.credits;
|
|
39
|
+
totalCredits += item.credits;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const cgpa = roundTo(totalWeightedPoints / totalCredits, 2);
|
|
43
|
+
const approximatePercentage = roundTo((cgpa / maxGradePoint) * 100, 2);
|
|
44
|
+
|
|
45
|
+
return { cgpa, totalCredits, approximatePercentage };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// 2. Attendance Calculator
|
|
49
|
+
function calculateAttendance(attendedClasses, totalClasses, targetPercentage = 75) {
|
|
50
|
+
assertFiniteNumber(attendedClasses, 'attendedClasses');
|
|
51
|
+
assertFiniteNumber(totalClasses, 'totalClasses');
|
|
52
|
+
assertFiniteNumber(targetPercentage, 'targetPercentage');
|
|
53
|
+
|
|
54
|
+
if (totalClasses <= 0) {
|
|
55
|
+
throw new Error('totalClasses must be greater than 0');
|
|
56
|
+
}
|
|
57
|
+
if (attendedClasses < 0 || attendedClasses > totalClasses) {
|
|
58
|
+
throw new RangeError('attendedClasses must be between 0 and totalClasses');
|
|
59
|
+
}
|
|
60
|
+
if (targetPercentage <= 0 || targetPercentage >= 100) {
|
|
61
|
+
throw new RangeError('targetPercentage must be between 0 and 100 (exclusive)');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const percentage = roundTo((attendedClasses / totalClasses) * 100, 2);
|
|
65
|
+
const ratio = targetPercentage / 100;
|
|
66
|
+
|
|
67
|
+
const maxSkippableClasses =
|
|
68
|
+
percentage >= targetPercentage
|
|
69
|
+
? Math.max(0, Math.floor(attendedClasses / ratio - totalClasses))
|
|
70
|
+
: 0;
|
|
71
|
+
|
|
72
|
+
const classesNeededForTarget =
|
|
73
|
+
percentage >= targetPercentage
|
|
74
|
+
? 0
|
|
75
|
+
: Math.max(0, Math.ceil((ratio * totalClasses - attendedClasses) / (1 - ratio)));
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
attendedClasses,
|
|
79
|
+
totalClasses,
|
|
80
|
+
percentage,
|
|
81
|
+
targetPercentage,
|
|
82
|
+
maxSkippableClasses,
|
|
83
|
+
classesNeededForTarget,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// 3. Eligibility Checker (default 75%)
|
|
88
|
+
function checkEligibility(attendedClasses, totalClasses, minimumPercentage = 75) {
|
|
89
|
+
const stats = calculateAttendance(attendedClasses, totalClasses, minimumPercentage);
|
|
90
|
+
const eligible = stats.percentage >= minimumPercentage;
|
|
91
|
+
const shortfallPercentage = eligible
|
|
92
|
+
? 0
|
|
93
|
+
: roundTo(minimumPercentage - stats.percentage, 2);
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
eligible,
|
|
97
|
+
currentPercentage: stats.percentage,
|
|
98
|
+
minimumPercentage,
|
|
99
|
+
shortfallPercentage,
|
|
100
|
+
classesNeeded: stats.classesNeededForTarget,
|
|
101
|
+
bunksAvailable: stats.maxSkippableClasses,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// 4. Percentage Calculator
|
|
106
|
+
function calculatePercentage(obtainedMarks, totalMarks) {
|
|
107
|
+
assertFiniteNumber(obtainedMarks, 'obtainedMarks');
|
|
108
|
+
assertFiniteNumber(totalMarks, 'totalMarks');
|
|
109
|
+
|
|
110
|
+
if (totalMarks <= 0) {
|
|
111
|
+
throw new Error('totalMarks must be greater than 0');
|
|
112
|
+
}
|
|
113
|
+
if (obtainedMarks < 0 || obtainedMarks > totalMarks) {
|
|
114
|
+
throw new RangeError('obtainedMarks must be between 0 and totalMarks');
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return roundTo((obtainedMarks / totalMarks) * 100, 2);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// 5. Grade Calculator
|
|
121
|
+
function calculateGrade(marks, maxMarks = 100) {
|
|
122
|
+
const pct = calculatePercentage(marks, maxMarks);
|
|
123
|
+
|
|
124
|
+
if (pct >= 90) return { percentage: pct, grade: 'O', gradePoint: 10 };
|
|
125
|
+
if (pct >= 80) return { percentage: pct, grade: 'A+', gradePoint: 9 };
|
|
126
|
+
if (pct >= 70) return { percentage: pct, grade: 'A', gradePoint: 8 };
|
|
127
|
+
if (pct >= 60) return { percentage: pct, grade: 'B+', gradePoint: 7 };
|
|
128
|
+
if (pct >= 50) return { percentage: pct, grade: 'B', gradePoint: 6 };
|
|
129
|
+
if (pct >= 40) return { percentage: pct, grade: 'C', gradePoint: 5 };
|
|
130
|
+
return { percentage: pct, grade: 'F', gradePoint: 0 };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// 6. Study Time Calculator
|
|
134
|
+
function calculateStudyTime({ totalSyllabusHours, completedHours = 0, daysRemaining }) {
|
|
135
|
+
assertFiniteNumber(totalSyllabusHours, 'totalSyllabusHours');
|
|
136
|
+
assertFiniteNumber(completedHours, 'completedHours');
|
|
137
|
+
assertFiniteNumber(daysRemaining, 'daysRemaining');
|
|
138
|
+
|
|
139
|
+
if (totalSyllabusHours <= 0) {
|
|
140
|
+
throw new Error('totalSyllabusHours must be greater than 0');
|
|
141
|
+
}
|
|
142
|
+
if (completedHours < 0 || completedHours > totalSyllabusHours) {
|
|
143
|
+
throw new RangeError('completedHours must be between 0 and totalSyllabusHours');
|
|
144
|
+
}
|
|
145
|
+
if (daysRemaining <= 0) {
|
|
146
|
+
throw new Error('daysRemaining must be greater than 0');
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const remainingHours = roundTo(totalSyllabusHours - completedHours, 2);
|
|
150
|
+
const completionPercentage = roundTo((completedHours / totalSyllabusHours) * 100, 2);
|
|
151
|
+
const recommendedDailyHours = roundTo(remainingHours / daysRemaining, 2);
|
|
152
|
+
|
|
153
|
+
return {
|
|
154
|
+
totalSyllabusHours,
|
|
155
|
+
completedHours,
|
|
156
|
+
remainingHours,
|
|
157
|
+
completionPercentage,
|
|
158
|
+
daysRemaining,
|
|
159
|
+
recommendedDailyHours,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
module.exports = {
|
|
164
|
+
calculateCGPA,
|
|
165
|
+
calculateAttendance,
|
|
166
|
+
checkEligibility,
|
|
167
|
+
calculatePercentage,
|
|
168
|
+
calculateGrade,
|
|
169
|
+
calculateStudyTime,
|
|
170
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
"name": "student-academic-toolkit",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Academic utility toolkit for students: CGPA, attendance, 75% eligibility, percentage, grades, and study time.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "node test.js"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [
|
|
10
|
+
"student",
|
|
11
|
+
"cgpa",
|
|
12
|
+
"attendance",
|
|
13
|
+
"grades",
|
|
14
|
+
"education"
|
|
15
|
+
],
|
|
16
|
+
"license": "MIT"
|
|
6
17
|
}
|
package/test.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
const assert = require('assert');
|
|
2
|
+
const {
|
|
3
|
+
calculateCGPA,
|
|
4
|
+
calculateAttendance,
|
|
5
|
+
checkEligibility,
|
|
6
|
+
calculatePercentage,
|
|
7
|
+
calculateGrade,
|
|
8
|
+
calculateStudyTime,
|
|
9
|
+
} = require('./index');
|
|
10
|
+
|
|
11
|
+
// 1. CGPA
|
|
12
|
+
const cgpaRes = calculateCGPA([
|
|
13
|
+
{ gradePoints: 9, credits: 4 },
|
|
14
|
+
{ gradePoints: 8, credits: 3 },
|
|
15
|
+
]);
|
|
16
|
+
assert.strictEqual(cgpaRes.cgpa, 8.57);
|
|
17
|
+
assert.strictEqual(cgpaRes.totalCredits, 7);
|
|
18
|
+
|
|
19
|
+
// 2. Attendance
|
|
20
|
+
const att = calculateAttendance(30, 40, 75);
|
|
21
|
+
assert.strictEqual(att.percentage, 75);
|
|
22
|
+
assert.strictEqual(att.maxSkippableClasses, 0);
|
|
23
|
+
|
|
24
|
+
// 3. Eligibility
|
|
25
|
+
const eligFail = checkEligibility(28, 40, 75);
|
|
26
|
+
assert.strictEqual(eligFail.eligible, false);
|
|
27
|
+
assert.strictEqual(eligFail.classesNeeded, 8);
|
|
28
|
+
|
|
29
|
+
const eligPass = checkEligibility(34, 40, 75);
|
|
30
|
+
assert.strictEqual(eligPass.eligible, true);
|
|
31
|
+
assert.strictEqual(eligPass.bunksAvailable, 5);
|
|
32
|
+
|
|
33
|
+
// 4. Percentage & 5. Grade
|
|
34
|
+
assert.strictEqual(calculatePercentage(85, 100), 85);
|
|
35
|
+
assert.deepStrictEqual(calculateGrade(92, 100), {
|
|
36
|
+
percentage: 92,
|
|
37
|
+
grade: 'O',
|
|
38
|
+
gradePoint: 10,
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// 6. Study Time
|
|
42
|
+
const study = calculateStudyTime({
|
|
43
|
+
totalSyllabusHours: 120,
|
|
44
|
+
completedHours: 30,
|
|
45
|
+
daysRemaining: 15,
|
|
46
|
+
});
|
|
47
|
+
assert.strictEqual(study.remainingHours, 90);
|
|
48
|
+
assert.strictEqual(study.recommendedDailyHours, 6);
|
|
49
|
+
|
|
50
|
+
console.log('All student-toolkit tests passed.');
|