JavaScript: Compute the average and grade of the students
JavaScript Conditional Statement and loops: Exercise-6 with Solution
Student Grades Calculation
Write a JavaScript program that computes the average marks of the following students. Then, this average is used to determine the corresponding grade.
Student Name | Marks |
---|---|
David | 80 |
Vinoth | 77 |
Divya | 88 |
Ishitha | 95 |
Thomas | 68 |
The grades are computed as follows:
Range | Grade |
---|---|
<60 | F |
<70 | D |
<80 | C |
<90 | B |
<100 | A |
Visual Presentation:

Sample Solution:
JavaScript Code:
// Array of students with their names and grades
var students = [['David', 80], ['Vinoth', 77], ['Divya', 88], ['Ishitha', 95], ['Thomas', 68]];
var Avgmarks = 0;
// Loop through the students array to calculate the total marks
for (var i = 0; i < students.length; i++) {
Avgmarks += students[i][1];
var avg = (Avgmarks / students.length);
}
// Calculate and log the average grade
console.log("Average grade: " + (Avgmarks) / students.length);
// Determine the grade based on the average
if (avg < 60) {
console.log("Grade: F");
} else if (avg < 70) {
console.log("Grade: D");
} else if (avg < 80) {
console.log("Grade: C");
} else if (avg < 90) {
console.log("Grade: B");
} else if (avg <= 100) {
console.log("Grade: A");
}
Output:
Average grade: 81.6 Grade : B
Flowchart:

Live Demo:
See the Pen javascript-conditional-statements-and-loops-exercise-6 by w3resource (@w3resource) on CodePen.
For more Practice: Solve these Related Problems:
- Write a JavaScript function that calculates the average marks of students and assigns grades using a switch statement.
- Write a JavaScript function that computes student grades from an array of marks and returns a summary object mapping grades to names.
- Write a JavaScript function that calculates the average mark and determines the corresponding grade without using if-else chains.
- Write a JavaScript function that validates student marks, computes the average, and assigns grades while handling missing scores gracefully.
Go to:
PREV : Odd or Even Loop.
NEXT : FizzBuzz.
Improve this sample solution and post your code through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.