Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Grade Calculator in JavaScript

Build a grade calculator in JavaScript that turns marks into A, B, C, D or F with if-else and switch(true). Rejects invalid marks, with explanation and output.

BeginnerJavaScript Conditional ProgramsExample 5 of 5
grade-calculator.js
1// Grade system:
2// 90-100: A
3// 80-89: B
4// 70-79: C
5// 60-69: D
6// Below 60: F
7
8function calculateGrade(marks) {
9 if (marks >= 90 && marks <= 100) {
10 return 'A';
11 } else if (marks >= 80 && marks < 90) {
12 return 'B';
13 } else if (marks >= 70 && marks < 80) {
14 return 'C';
15 } else if (marks >= 60 && marks < 70) {
16 return 'D';
17 } else if (marks >= 0 && marks < 60) {
18 return 'F';
19 } else {
20 return 'Invalid marks';
21 }
22}
23
24// Test cases
25console.log("95 marks:", calculateGrade(95));
26console.log("85 marks:", calculateGrade(85));
27console.log("75 marks:", calculateGrade(75));
28console.log("65 marks:", calculateGrade(65));
29console.log("45 marks:", calculateGrade(45));
30console.log("105 marks:", calculateGrade(105));
31
32// Method 2: Using switch with ranges
33function calculateGrade2(marks) {
34 switch(true) {
35 case marks >= 90 && marks <= 100:
36 return 'A';
37 case marks >= 80 && marks < 90:
38 return 'B';
39 case marks >= 70 && marks < 80:
40 return 'C';
41 case marks >= 60 && marks < 70:
42 return 'D';
43 case marks >= 0 && marks < 60:
44 return 'F';
45 default:
46 return 'Invalid marks';
47 }
48}
49
50console.log("\nUsing switch:");
51console.log("92 marks:", calculateGrade2(92));
52console.log("78 marks:", calculateGrade2(78));

Sample Output

95 marks: A
85 marks: B
75 marks: C
65 marks: D
45 marks: F
105 marks: Invalid marks

Using switch:
92 marks: A
78 marks: C

In this one

  • How else if chains stop at the first match
  • Why the order of checks matters
  • How to reject values outside 0 to 100
  • How switch(true) works with conditions

How it works

  1. 1

    calculateGrade(95) matches the first branch and returns A.

  2. 2

    85, 75, 65 and 45 match B, C, D and F.

  3. 3

    105 matches nothing and returns Invalid marks.

  4. 4

    switch(true) gives A for 92 and C for 78.

Edge cases

Marks above 100

Without an upper check, 107 becomes A.

Keep marks <= 100 in the A condition, or validate before grading.

Decimal marks like 89.5

89.5 is not >= 90, so it gets B.

Decide a rounding rule first, for example Math.round(marks).

Text input

"85" works with >=, but "abc" becomes Invalid marks.

Convert input with Number() and handle NaN clearly.

What's going on

This program turns exam marks into a letter grade. 90 to 100 is an A, 80 to 89 a B, 70 to 79 a C, 60 to 69 a D, and below 60 an F. Report cards and result portals do exactly this.

Method 1 uses an else if chain. JavaScript checks each condition from top to bottom and stops at the first one that is true. That is why the order matters. Test the highest grade first. If you checked marks >= 60 first with no upper limit, a 95 would match it and get a D.

Each branch here also has an upper limit, like marks >= 80 && marks < 90. That makes the ranges clear and stops overlaps.

Check the valid range too. Marks must be between 0 and 100. If you skip that, a typo like 107 would pass marks >= 90 and get an A. Here, 105 falls through every branch and the final else returns "Invalid marks".

Method 2 uses switch(true). Normally switch compares one value to fixed cases. With switch(true), each case is a condition. The first case that is true wins. Some people like how it looks as a table. It works the same as the if-else chain.

The test marks 95, 85, 75, 65, 45 and 105 cover every grade and the invalid case.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What grade does calculateGrade(72) return?

Pick an answer to check it.

Question 2

What does calculateGrade(-5) return?

Pick an answer to check it.

Questions people ask

Why do the ranges need both ends?

Both ends make each grade band clear and stop a value from matching two bands.

Is switch(true) better than if-else?

No. It is a style choice. Most code uses if-else.

How do I add an A+ grade?

Add a branch for marks >= 95 before the A branch.