DEV Community

Krishna
Krishna

Posted on

How I Built a VTU SGPA Calculator Using JavaScript

How I Built a VTU SGPA Calculator Using JavaScript

Many VTU students struggle to calculate their SGPA manually after results are announced.

To make this easier, I built a simple VTU SGPA Calculator using JavaScript and a JSON data file.

In this post, I’ll explain how you can build a basic SGPA calculator using JavaScript and JSON.


SGPA Formula

VTU SGPA is calculated using a credit-weighted average formula.

SGPA = Σ (Credit × Grade Point) / Σ Credits
Enter fullscreen mode Exit fullscreen mode

Example

  • Mathematics — 4 credits — Grade A (9 points)
  • Physics — 3 credits — Grade B (8 points)

JSON Data File for Subjects

Instead of hardcoding subjects in JavaScript, we store them in a JSON file.

subjects.json

[
  { "subject": "Mathematics", "credits": 4 },
  { "subject": "Physics", "credits": 3 },
  { "subject": "Chemistry", "credits": 3 },
  { "subject": "Programming", "credits": 4 }
]
Enter fullscreen mode Exit fullscreen mode

This approach makes the calculator:

  • easier to maintain
  • easy to update subjects
  • reusable for different semesters

HTML Structure

<h2>VTU SGPA Calculator</h2>

<div id="subjects"></div>

<button onclick="calculateGPA()">Calculate SGPA</button>

<p id="result"></p>
Enter fullscreen mode Exit fullscreen mode

This creates a simple interface where students can enter their grade points.


Loading Subjects from JSON

fetch("subjects.json")
  .then(res => res.json())
  .then(data => {

    const container = document.getElementById("subjects");

    data.forEach(sub => {

      container.innerHTML += `
        <div>
          ${sub.subject} (${sub.credits} credits)
          <input type="number" min="0" max="10" class="grade">
        </div>
      `;

    });

  });
Enter fullscreen mode Exit fullscreen mode

This dynamically loads the subject list and creates input fields.


SGPA Calculation Logic

function calculateGPA(){

  const grades = document.querySelectorAll(".grade");

  let totalCredits = 0;
  let weightedSum = 0;

  grades.forEach((input, index) => {

    const grade = parseFloat(input.value) || 0;
    const credits = subjects[index].credits;

    weightedSum += grade * credits;
    totalCredits += credits;

  });

  const gpa = weightedSum / totalCredits;

  document.getElementById("result").innerText =
    "Your SGPA: " + gpa.toFixed(2);

}
Enter fullscreen mode Exit fullscreen mode

This calculates SGPA using the credit-weighted formula.


Why Use JSON?

Using JSON helps because:

  • subjects can be updated easily
  • the calculator works for different semesters
  • the structure is scalable

Try the Full Tool

If you're a VTU student, you can try the full calculator here:

https://www.civvy.tech/vtu-sgpa


Conclusion

Building a VTU SGPA calculator with JavaScript and JSON is simple and efficient.

It helps students:

  • calculate SGPA quickly
  • avoid manual calculation errors
  • maintain and expand the calculator easily

Top comments (0)