Dev.to WebDev 🛠 Dev 👁 0 📖 1 min read

How to Calculate a Weighted Grade (and the Score You Need on the Final) in JavaScript

If your course has weighted categories (homework 20%, quizzes 30%, final 50%), a plain average gives the wrong answer. Here is how to compute a weighted grade correctly, and the one question most students need answered:

If your course has weighted categories (homework 20%, quizzes 30%, final 50%), a plain average gives the wrong answer. Here is how to compute a weighted grade correctly, and the one question most students need answered: what score do I need on the final?

Weighted average

Multiply each category score by its weight, add them up, and divide by the total weight. Dividing by the total weight matters when the weights do not add up to 100 yet, for example mid-semester.

function weightedGrade(items) {
  // items: [{ score: 88, weight: 20 }, ...]
  const totalWeight = items.reduce((s, i) => s + i.weight, 0);
  if (totalWeight === 0) return null;
  const sum = items.reduce((s, i) => s + i.score * i.weight, 0);
  return sum / totalWeight;
}

weightedGrade([
  { score: 90, weight: 20 },
  { score: 80, weight: 30 },
  { score: 70, weight: 50 },
]); // 77

What do I need on the final?

Let current be your grade so far on the weight already graded (doneWeight), and finalWeight the weight of the final. To end with a target:

function neededOnFinal(current, doneWeight, finalWeight, target) {
  const earned = current * doneWeight;           // points earned so far
  const need = target * (doneWeight + finalWeight) - earned;
  return need / finalWeight;                     // required final score
}

neededOnFinal(80, 50, 50, 85); // 90

If the result is above 100, the target is out of reach without extra credit. If it is below 0, you have already secured it.

Edge cases

  • Weights that do not sum to 100: normalise by the total, as above.
  • Empty categories: skip them instead of counting them as zero.
  • Dropped lowest score: sort the category scores and remove the lowest before averaging.
  • Rounding: round only the final display, not the intermediate numbers.

Try it without coding

I keep a free grade calculator that does these calculations in the browser at https://thegradecalculator.tech/ . Check your syllabus for how your instructor rounds and weights, since policies differ.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.