Start learning free

LESSON 199 OF 671 · PROJECTS: EVERYDAY PROGRAMS

Report Card V2 in JavaScript: Weighted Score

Practice Read the full explanation

Combine variables and arithmetic to compute a student's weighted final score.

Weights: Homework 20%, Midterm 30%, Final 50%. Each weighted part is rounded down to a whole number before the parts are added.

You start with:

const homework = 90;
const midterm = 82;
const final_ = 88;

Build:

Score: 86

Rules:

  • Compute score = Math.floor(homework * 20 / 100) + Math.floor(midterm * 30 / 100) + Math.floor(final_ * 50 / 100).
  • Each part is rounded down with Math.floor (the third variable is named final_).
  • For the starting data: 18 + 24 + 44 = 86.
  • Print "Score: " followed by the score, for example console.log("Score: " + score);

The third variable is named final_. Wrap each part in Math.floor(...), for example Math.floor(homework * 20 / 100).

const homework = 90;
const midterm = 82;
const final_ = 88;

const score = Math.floor(homework * 20 / 100) + Math.floor(midterm * 30 / 100) + Math.floor(final_ * 50 / 100);
console.log("Score: " + score);

STEP 1 OF 2
    Ctrl + Enter to check

    JAVASCRIPT · PROJECTS: EVERYDAY PROGRAMS

    Report Card V2 in JavaScript, explained

    Problem overview

    Version 2 stops treating all marks equally. A student's final score combines three parts with different weights: homework counts for 20%, the midterm for 30% and the final exam for 50%. Each weighted part is rounded down to a whole number, and then the three parts are added. The program prints the total as Score: 86.

    The starter variables are homework = 90, midterm = 82 and final_ = 88. The trailing underscore in final_ is part of the name the lesson uses, so keep it. The parts work out to 18, 24 and 44, which add up to 86.

    Approach: weight each part, round it, then add

    A percentage weight means "mark times weight, divided by 100". For homework that is 90 * 20 / 100, which is 18. For the midterm, 82 * 30 / 100 is 24.6, and for the final exam 88 * 50 / 100 is 44.

    The midterm shows why the order of rounding matters. The lesson rounds each part before adding, so 24.6 becomes 24 and the total is 18 + 24 + 44 = 86. Adding the exact values first would give 86.6, and the single rounding that follows happens to give the same 86 here, but not always. With 85, 75 and 95 the parts are 17, 22.5 and 47.5. Rounding each part first gives 17 + 22 + 47 = 86, while adding first gives 87. The lesson's rule is to round each part, and the code follows it.

    Step by step

    1. Create score with const. It is computed once from the three starter variables.
    2. Work out each part on its own. Multiply the mark by its weight and divide by 100, and wrap that in Math.floor so the part is a whole number. The weights 20, 30 and 50 add up to 100, so the three parts together make up the whole score.
    3. Add the three rounded parts with +.
    4. Print the result with console.log, joining the label "Score: " and the number.

    The solution in JavaScript

    const homework = 90;
    const midterm = 82;
    const final_ = 88;
    
    const score = Math.floor(homework * 20 / 100) + Math.floor(midterm * 30 / 100) + Math.floor(final_ * 50 / 100);
    console.log("Score: " + score);
    

    Notes on the expression:

    • Left to right. * and / have the same precedence and run from left to right, so homework * 20 / 100 multiplies first and divides second. Multiplying first keeps the intermediate value a whole number (1800 here) before the division.
    • Three separate Math.floor calls. Each one covers exactly one part. Writing one Math.floor around the whole sum would round only once, at the end.
    • Why not 0.2 * homework? Decimal fractions such as 0.2 and 0.3 can't be stored exactly in binary floating point. Multiplying by the whole-number weight and dividing by 100 gives the exact answers 18, 24.6 and 44 for this data, with no surprises.

    Edge cases and common mistakes

    • Rounding once at the end. As the 85, 75, 95 example shows, Math.floor around the whole sum gives 87, while rounding each part gives 86.
    • Misspelling final_. Writing final refers to a variable that doesn't exist, and the program stops with a ReferenceError.
    • Weights that don't add up. Using 20, 30 and 40 loses 10% of the score. A mark of 100 in all three parts should give 100.
    • Dividing by 10 instead of 100. A weight written as a percentage needs / 100.

    Testing

    • 90, 82, 88 gives Score: 86.
    • 85, 75, 95 gives Score: 86.
    • 100, 100, 100 gives Score: 100.
    • 0, 0, 0 gives Score: 0.
    • 99, 99, 99 gives Score: 97, one point under 99 because each part loses its fraction.
    Keep going with JavaScript650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free