Start learning free

LESSON 202 OF 289 · PROJECTS: EVERYDAY PROGRAMS

Report Card V2 in TypeScript: Weighted Score with Math.floor

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: number = 90;
const midterm: number = 82;
const final_: number = 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: number = 90;
const midterm: number = 82;
const final_: number = 88;

const score: number = 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

    TYPESCRIPT · PROJECTS: EVERYDAY PROGRAMS

    Report Card V2 in TypeScript, explained

    Problem overview

    Version 2 of the Report Card project computes a student's weighted final score. Three marks are stored in constants: homework, midterm and final_. They count for 20%, 30% and 50% of the score. The third name ends with an underscore, so the code can use final_ without trouble.

    Each weighted part is rounded down to a whole number before the parts are added. With the starting marks 90, 82 and 88:

    • homework: 90 times 20 divided by 100 is 18;
    • midterm: 82 times 30 divided by 100 is 24.6, which rounds down to 24;
    • final: 88 times 50 divided by 100 is 44.

    The sum is 86, and the program prints:

    Score: 86
    

    The previous version already averaged scores with Math.floor. This one adds weights.

    Approach: one weighted part per mark

    A weight of 20% means "20 out of every 100", so a part is the mark multiplied by the weight and divided by 100. The three weights add up to 100, so together the three parts make up the whole score.

    The order of operations is deliberate: multiply first, then divide. homework * 20 / 100 is read left to right, as (homework * 20) / 100. The product is an exact integer, and a single division follows. Each part is then wrapped in its own Math.floor, because TypeScript's number is a floating-point value and / keeps the fraction: 82 * 30 / 100 evaluates to 24.6, not 24.

    Where the Math.floor goes is part of the specification. Rounding each part separately can give a different answer than rounding the total once. With every mark equal to 5, the three parts are 1, 1.5 and 2.5. Floor each part and the score is 4. Add the exact parts first and floor the total, and the score is 5. The lesson wants the first: round each part, then add.

    Step by step

    1. Declare the result. Below the three marks, create score as a const of type number. It is computed once, so it never needs to change.
    2. Compute the homework part. Multiply homework by 20, divide by 100 and round down with Math.floor.
    3. Compute the midterm part in the same way, with weight 30 and midterm.
    4. Compute the final part with weight 50 and final_.
    5. Add the three parts with +. All three go in one expression, each part rounded on its own.
    6. Print the result. Join "Score: " and score with + inside console.log. The tests check what the program prints.

    The solution in TypeScript

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

    Details worth knowing:

    • No parentheses needed. Multiplication and division share a precedence level and run left to right, and both bind tighter than +. Each Math.floor(...) call is evaluated before the three results are added.
    • Why not multiply by 0.2. The weights are written as whole percents, and the product is divided by 100 only at the end. That keeps the multiplication exact, because every mark and weight is an integer.
    • The name final_. Use the lesson's name exactly. The trailing underscore keeps the constant distinct from the word final.
    • Types are erased. The : number annotations help the compiler check the code, but they disappear in the JavaScript that runs.

    Edge cases and common mistakes

    • Flooring the total instead of each part. Math.floor(homework * 20 / 100 + midterm * 30 / 100 + final_ * 50 / 100) prints 86 for the starting marks, so the mistake hides. It shows up with other data: marks of 5, 5 and 5 give 5 that way, while the lesson's rule gives 4.
    • Forgetting a Math.floor. The midterm part is the only one with a fraction in the starting data (24.6). Leave its floor out and the sum becomes 86.6, which prints as Score: 86.6.
    • Mixing up marks and weights. The weights stay with their marks: 20 goes with homework, 30 with midterm and 50 with final_. Swapping two of them still prints a number, just the wrong one.
    • Perfect marks. With 100, 100 and 100 every part divides evenly (20, 30 and 50), and the score is 100.

    Testing

    • Marks 90, 82, 88 print Score: 86.
    • Marks 55, 82, 88 print Score: 79.
    • Marks 100, 100, 100 print Score: 100.
    • Marks 5, 5, 5 print Score: 4.
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free