Grade Calculator
Program to calculate grade based on marks
JavaScript Grade Calculator Program
This program helps you to learn the fundamental structure and syntax of JavaScript programming.
// Grade system:
// 90-100: A
// 80-89: B
// 70-79: C
// 60-69: D
// Below 60: F
function calculateGrade(marks) {
if (marks >= 90 && marks <= 100) {
return 'A';
} else if (marks >= 80 && marks < 90) {
return 'B';
} else if (marks >= 70 && marks < 80) {
return 'C';
} else if (marks >= 60 && marks < 70) {
return 'D';
} else if (marks >= 0 && marks < 60) {
return 'F';
} else {
return 'Invalid marks';
}
}
// Test cases
console.log("95 marks:", calculateGrade(95));
console.log("85 marks:", calculateGrade(85));
console.log("75 marks:", calculateGrade(75));
console.log("65 marks:", calculateGrade(65));
console.log("45 marks:", calculateGrade(45));
console.log("105 marks:", calculateGrade(105));
// Method 2: Using switch with ranges
function calculateGrade2(marks) {
switch(true) {
case marks >= 90 && marks <= 100:
return 'A';
case marks >= 80 && marks < 90:
return 'B';
case marks >= 70 && marks < 80:
return 'C';
case marks >= 60 && marks < 70:
return 'D';
case marks >= 0 && marks < 60:
return 'F';
default:
return 'Invalid marks';
}
}
console.log("\nUsing switch:");
console.log("92 marks:", calculateGrade2(92));
console.log("78 marks:", calculateGrade2(78));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
Understanding Grade Calculator
This program demonstrates range-based conditional logic.
Grade System
Common grading scale:
-
A
: 90-100 (Excellent)
-
B
: 80-89 (Good)
-
C
: 70-79 (Average)
-
D
: 60-69 (Below Average)
-
F
: 0-59 (Fail)
Method 1: If-Else Chain
Checking ranges with AND operator:
if (marks >= 90 && marks <= 100) {
} else if (marks >= 80 && marks < 90) {
return 'B';
}
// ... and so on
return 'A';Range Checking
marks >= 90 && marks <= 100: Inclusive rangemarks >= 80 && marks < 90: Lower inclusive, upper exclusiveMethod 2: Switch with Ranges
Using switch(true) pattern:
switch(true) {
case marks >= 90 && marks <= 100:
case marks >= 80 && marks < 90:
return 'B';
// ...
}
return 'A';Switch(true) Pattern
Input Validation
Always validate input:
if (marks < 0 || marks > 100) {
}
return 'Invalid marks';Real-world Applications
When to Use:
-
If-else
: Most common, readable
-
Switch
: Multiple ranges, organized
Let us now understand every line and the components of the above program.
Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.
Practical Learning Notes for Grade Calculator
This JavaScript program is part of the "Conditional Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.