Dev.to WebDev πŸ›  Dev πŸ‘ 0 πŸ“– 3 min read

Why My Quiz Can Show 4/12 and 100% at the Same Time

A quiz result can show 4/12 points and 100% of total points on the same line. Give Pride four points and every other category zero. Pride earned four of its 12 possible points, and it accounts for all four points earned

A quiz result can show 4/12 points and 100% of total points on the same line. Give Pride four points and every other category zero. Pride earned four of its 12 possible points, and it accounts for all four points earned in that attempt.

This example comes from 7 Deadly Sins Test, a browser quiz I maintain. It uses 28 statements about recent habits as a self-reflection exercise. Each of the seven categories has four statements worth 0–3 points apiece. A category can earn up to 12 points, and the full attempt can earn up to 84. Project scoring rules

A score can stay the same while its share drops

Give Envy eight points on a second attempt while leaving Pride at four. The total rises to 12, so Pride’s share falls from 100% to 33%. Its raw score stays at 4/12.

Attempt Pride Envy Total across all seven categories Pride’s share
A 4/12 0/12 4 100%
B 4/12 8/12 12 33%

Use the raw score to compare a category across attempts. Use its share to see how the points were distributed within one attempt. A label like 33% of total points makes that second denominator visible.

Store one answer per statement

The quiz keeps the current answer for each statement in a browser-side array. If someone goes back and changes a 3 to a 1, the new value replaces the old one. On submission, that statement contributes one point.

An empty slot represents an unanswered statement. The number 0 represents a real answer worth zero points. Keeping those states distinct lets the quiz score an attempt in which someone deliberately chose zero on all 28 statements.

Compute the result rows once

This JavaScript example takes the seven category totals from a completed attempt. Each input is an integer from 0 to 12. It ranks the rows by raw points and marks every category tied for the highest positive score.

function summarizeScores(scores) {
  const values = Object.values(scores)
  const total = values.reduce((sum, points) => sum + points, 0)
  const highest = Math.max(...values)

  return Object.entries(scores)
    .map(([category, points]) => ({
      category,
      points,
      sharePercent: total > 0 ? Math.round((points * 100) / total) : null,
      isLeader: total > 0 && points === highest,
    }))
    .sort((first, second) => second.points - first.points)
}

const scores = {
  Pride: 4,
  Greed: 0,
  Lust: 0,
  Envy: 8,
  Gluttony: 0,
  Wrath: 0,
  Sloth: 0,
}

console.table(summarizeScores(scores))

Three cases are worth checking before wiring up the UI. The table lists the nonzero scores, with every other category at zero.

Scores Expected display
All zero Every row shows 0/12 and a dash for its share. There are no leaders.
1, 1, 1 All three categories lead. They each display 33%, for a rounded sum of 99%.
1, 1, 4 The four-point category leads. The displayed shares are 17%, 17%, and 67%, for a rounded sum of 101%.

A dash means the share is undefined because the total is zero. When the total is positive, a zero-point category displays 0%. Independently rounded labels can sum to 99% or 101%, so the quiz keeps raw points for ranking and rounds shares for display. MDN on Math.round()

Keep the meaning when the result leaves the page

The web result and downloadable PNG use the same computed rows. Both display points out of 12 and the rounded share. On the web, the diamond beside each leading category also has the accessible name β€œHighest score,” so the highlight carries meaning beyond its color. W3C guidance on use of color

If people mainly use your quiz to compare their own attempts over time, would you show both numbers upfront or put the share in a breakdown?

πŸ“° 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.