Start learning free

LESSON 201 OF 289 · PROJECTS: EVERYDAY PROGRAMS

Report Card V1 in TypeScript: Integer Average with Math.floor

Practice Read the full explanation

Compute a student's average score across three tests using integer division so the result is a whole number.

You start with:

const test1: number = 85;
const test2: number = 92;
const test3: number = 78;

Build:

Average: 85

Rules:

  • Compute the integer average of test1, test2, and test3 using Math.floor to drop the fraction (a plain / gives a decimal).
  • Print "Average: " followed by the average, for example console.log("Average: " + average);

Use Math.floor((test1 + test2 + test3) / 3) to get an integer average.

const test1: number = 85;
const test2: number = 92;
const test3: number = 78;

const average: number = Math.floor((test1 + test2 + test3) / 3);
console.log("Average: " + average);

STEP 1 OF 2
    Ctrl + Enter to check

    TYPESCRIPT · PROJECTS: EVERYDAY PROGRAMS

    Report Card V1 in TypeScript, explained

    Problem overview

    This is the first version of the Report Card project. A student has taken three tests, stored in the constants test1, test2 and test3. The program computes the average of the three scores as a whole number and prints it in the form Average: 85.

    With the starting scores 85, 92 and 78, the sum is 255 and 255 divided by 3 is exactly 85, so the program prints:

    Average: 85
    

    Later versions of the project build on this one.

    Approach: add, divide, round down

    An average is the sum divided by the count, so the program needs only one expression: add the three scores, divide by 3, and round the result down to a whole number.

    The rounding matters because TypeScript has a single number type. number is a floating-point value, so / never does integer division: 7 / 2 is 3.5, and an average that doesn't divide evenly comes out with a fraction. To get a whole number you ask for it, and Math.floor does that by rounding down to the nearest integer.

    Take the scores 80, 92 and 78. Their sum is 250, and the three expressions below behave differently:

    const a: number = 80, b: number = 92, c: number = 78;
    console.log((a + b + c) / 3);
    console.log(Math.floor((a + b + c) / 3));
    console.log(Math.floor(a + b + c / 3));
    

    They print:

    83.33333333333333
    83
    198
    

    The first keeps the fraction. The second is the intended result. The third shows the usual slip, which comes next in the mistakes section.

    Step by step

    1. Declare the result. Below the three starting constants, create average as a const of type number. It is computed once and never reassigned, so const fits.
    2. Add first, then divide. The sum goes in parentheses: (test1 + test2 + test3). Division binds tighter than addition, so without the parentheses only test3 would be divided by 3.
    3. Round down. Wrap the whole division in Math.floor(...), so the stored value is a whole number.
    4. Print it. Call console.log with "Average: " joined to average by +. The tests read what the program prints, so the value has to be printed, not just stored.

    The solution in TypeScript

    const test1: number = 85;
    const test2: number = 92;
    const test3: number = 78;
    
    const average: number = Math.floor((test1 + test2 + test3) / 3);
    console.log("Average: " + average);
    

    Details worth knowing:

    • Annotations. : number tells the compiler what each constant holds. The annotations are erased when TypeScript compiles to JavaScript, so they don't change what runs.
    • Math.floor returns a number. The result has type number, just like the input, which is why it fits the average annotation.
    • Joining text and a number. In "Average: " + average, the string on the left makes + concatenate, and the number is converted to text automatically.
    • Rounding down, not to the nearest. Math.floor drops the fraction by always rounding toward negative infinity. For test scores, which are never negative, that means the fractional part is simply cut off.

    Edge cases and common mistakes

    • Missing parentheses. Math.floor(test1 + test2 + test3 / 3) divides only the last score. With 80, 92 and 78 it prints 198 instead of 83.
    • Skipping the rounding. A plain / prints 83.33333333333333 for 80, 92 and 78. The starting scores happen to sum to 255, which divides evenly by 3, so the mistake only shows when the data changes. Write the code so it works for any scores.
    • Whole averages stay whole. When the sum divides evenly, Math.floor changes nothing. With 100, 100 and 99 the average is 99, and with 85, 92 and 0 it is 59.
    • Forgetting the space. The label is "Average: ", with a space after the colon, and the tests compare the printed text.

    Testing

    • Scores 85, 92, 78 print Average: 85.
    • Scores 80, 92, 78 print Average: 83.
    • Scores 100, 100, 99 print Average: 99.
    • Scores 85, 92, 0 print Average: 59.
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free