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
- Create
scorewithconst. It is computed once from the three starter variables. - Work out each part on its own. Multiply the mark by its weight and divide by 100, and wrap that in
Math.floorso 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. - Add the three rounded parts with
+. - 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, sohomework * 20 / 100multiplies first and divides second. Multiplying first keeps the intermediate value a whole number (1800 here) before the division. - Three separate
Math.floorcalls. Each one covers exactly one part. Writing oneMath.flooraround 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.flooraround the whole sum gives 87, while rounding each part gives 86. - Misspelling
final_. Writingfinalrefers to a variable that doesn't exist, and the program stops with aReferenceError. - 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.
