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.
Here is what you need
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 calculatePercentage = (subTotal) => {
if (subTotal === 0) return { percentage: 0, GP: 0 };
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 subjectTotal = Object.entries(value).reduce((result, [key, value], currentIndex, array) => {
const [subjectName, subjectPosition, suffix] = key.split('_')
const fullSubjectName = `${subjectName}_${subjectPosition}`
if(!result[fullSubjectName]) {
result[fullSubjectName] = 0;
}
result[fullSubjectName] += value;
if (currentIndex + 1 === array.length){
result.math_percentage = calculatePercentage(result.math_1st + result.math_2nd);
result.english_percentage= calculatePercentage(result.english_2nd + result.english_1st);
}
return result
}, {})
console.log(subjectTotal);
In the above code I use the calculatePercentage only in the last iteration of reduce for last element where all the elements of the array that you have provided are already reduced to result that you want. Then we attach to result the percentages according to the already final calculated values it contains.
Also as you can see in signature reduce((result, [key, value], currentIndex, array) reduce allows as parameter the currentIndex which will show which iteration the reduce method is applied to, and also the array that that was provided as input to be reduced.