I have a function subjectTotal which returns the total from a subject CQ and MCQ, I am trying to calculate the percentage inside the subjectTotal function, I have already a percentage function that returns the percentage and grade point.
SubjectTotal Function:
const value = {
english_1st_CQ: 5,
english_2nd_CQ: 10,
math_1st_CQ: 15,
math_1st_MCQ: 20,
math_2nd_CQ: 25,
math_2nd_MCQ: 30,
};
const subjectTotal = Object.entries(value).reduce((result, [key, value]) => {
const [subjectName, subjectPosition, suffix] = key.split('_')
const fullSubjectName = `${subjectName}_${subjectPosition}`
if(!result[fullSubjectName]) {
result[fullSubjectName] = 0;
}
result[fullSubjectName] += value
return result
}, {})
console.log(subjectTotal)
Calculate percentage function, also I am calculating the Grade point inside the function
const subjectTotal = {
english_1st: 5,
english_2nd: 10,
math_1st: 35,
math_2nd: 55
}
const calculatePercentage = (subTotal) => {
if (subTotal === 0) return { percentage: 0, GP: 0 };
// console.log(subTotal);
const percentage = (subTotal / 100) * 100 - 4;
let GP;
if (percentage >= 80) GP = 5;
if (percentage >= 70 && percentage <= 79) GP = 4;
if (percentage >= 60 && percentage <= 69) GP = 3.5;
if (percentage >= 50 && percentage <= 59) GP = 3;
if (percentage >= 40 && percentage <= 49) GP = 2;
if (percentage >= 33 && percentage <= 39) GP = 1;
if (percentage < 33) GP = 0;
return { percentage, GP };
};
const math_percentage = calculatePercentage(subjectTotal.math_1st + subjectTotal.math_2nd);
const english_percentage= calculatePercentage(subjectTotal.english_2nd + subjectTotal.english_1st);
console.log(math_percentage, english_percentage)
I don't want to call the function again and again, like this way math_percentage,english_percentage. so I am trying to call the calculatePercentage inside the subjectTotal and the subject total will return the subject total also the percentage
I don't have an example I just want to calculate the percentage inside the subTotal.