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
- Declare the result. Below the three starting constants, create
averageas aconstof typenumber. It is computed once and never reassigned, soconstfits. - Add first, then divide. The sum goes in parentheses:
(test1 + test2 + test3). Division binds tighter than addition, so without the parentheses onlytest3would be divided by 3. - Round down. Wrap the whole division in
Math.floor(...), so the stored value is a whole number. - Print it. Call
console.logwith"Average: "joined toaverageby+. 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.
: numbertells the compiler what each constant holds. The annotations are erased when TypeScript compiles to JavaScript, so they don't change what runs. Math.floorreturns a number. The result has typenumber, just like the input, which is why it fits theaverageannotation.- 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.floordrops 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
/prints83.33333333333333for 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.floorchanges 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.
