What this exercise practises
Three grades are stored in variables, mathGrade, englishGrade and scienceGrade. The program computes their average and prints whether the student passed. A grade of 5 or more is a pass.
The exercise combines three small ideas that appear in almost every program: arithmetic with the right grouping, a comparison that produces a boolean, and an if/else that picks one of two actions based on that boolean.
Step 1: computing the average
The average is the sum of the grades divided by how many there are. In code the sum must be wrapped in parentheses:
let mathGrade: number = 7;
let englishGrade: number = 4;
let scienceGrade: number = 9;
console.log((mathGrade + englishGrade + scienceGrade) / 3);
console.log(mathGrade + englishGrade + scienceGrade / 3);
6.666666666666667
14
Division binds tighter than addition, exactly as in school arithmetic. Without parentheses, only scienceGrade is divided by 3, and the result, 14, is not an average at all. The parentheses make the whole sum the left side of the division.
Two TypeScript details show up here:
- Every number is a double-precision floating-point value. There is no separate integer type, so
/keeps the fractional part:20 / 3is6.666666666666667, not 6. Languages with an integer type would truncate here. In TypeScript you only get a whole number if you ask for it withMath.floororMath.trunc, and this exercise does not need it: the exact average is what gets compared with 5. - The annotation
: numberis for the compiler.let grade: number = ...andlet grade = ...behave the same, because TypeScript infers the type from the value. When the file is compiled to JavaScript, the annotations are erased and nothing about them exists at run time.
Step 2: the condition
A pass is a grade of 5 or more, so the comparison is grade >= 5. The expression has the value true or false, and an if statement runs its block only when that value is true. The operator is >=, not >: a student whose average is exactly 5 must pass, and > would fail them.
Steps 3 and 4: printing the result
Inside the if block, console.log("Passed the test") prints the message on its own line. The else block, written as } else { on the line where the if block closes, prints "Failed the test". An else has no condition of its own. It runs whenever the if condition was false, so exactly one of the two messages is printed every time.
The solution in TypeScript
let mathGrade: number = 7;
let englishGrade: number = 4;
let scienceGrade: number = 9;
let grade: number = (mathGrade + englishGrade + scienceGrade) / 3;
if (grade >= 5) {
console.log("Passed the test");
} else {
console.log("Failed the test");
}
With the starting grades, the average is 6.67, which is at least 5, so the program prints:
Passed the test
Try other values in the editor above. Setting the three grades to 2, 4 and 3 gives an average of exactly 3, and the program prints Failed the test. Setting all three to 5 gives an average of exactly 5, and it prints Passed the test.
Common mistakes
- Forgetting the parentheses around the sum. The program still runs and still prints a message, so the mistake hides easily. It only shows when the printed decision is wrong for some grades.
- Using
>instead of>=. The result is the same for most inputs and wrong for an average of exactly 5. - Dividing by the wrong number. There are three grades, so the divisor is
3. If you add a fourth subject later, the divisor has to change with it. - Using
=in the condition. A single=assigns; comparing needs>=,===or similar.if (grade = 5)overwritesgradewith 5 instead of comparing it. A number is accepted as a condition, so the compiler does not stop you. - Putting a condition after
else.else (grade < 5)is a syntax error. If you want a second condition, the form iselse if (...). - Printing without
console.log. Typing a string on its own line compiles but prints nothing.
Key takeaways
- Parentheses control the order of operations, and division happens before addition.
- TypeScript numbers are floating-point, so
/keeps the fraction. - A comparison like
grade >= 5is a boolean expression, which is whatifneeds. if/elseruns exactly one of its two blocks.- Type annotations help the compiler and disappear in the compiled JavaScript.