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
- Declare the result. Below the three marks, create
scoreas aconstof typenumber. It is computed once, so it never needs to change. - Compute the homework part. Multiply
homeworkby 20, divide by 100 and round down withMath.floor. - Compute the midterm part in the same way, with weight 30 and
midterm. - Compute the final part with weight 50 and
final_. - Add the three parts with
+. All three go in one expression, each part rounded on its own. - Print the result. Join
"Score: "andscorewith+insideconsole.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
+. EachMath.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 wordfinal. - Types are erased. The
: numberannotations 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 asScore: 86.6. - Mixing up marks and weights. The weights stay with their marks: 20 goes with
homework, 30 withmidtermand 50 withfinal_. 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.
