Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

204
Vistas
how to calculate total percentage, total number with subject by using reduce

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.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda