GPA Calculator using React
Last Updated :
23 Jul, 2025
GPA Calculator is an application that provides a user interface for calculating and displaying a student's GPA(Grade Point Average). Using functional components and state management, this program enables users to input course information, including course name, credit hours and earned grades and adds them to a list dynamically. Users can also delete an individual list item from the course list. This application is implemented using Reactjs and provides a simple and responsive user interface to users.
Preview of Final Output:
GPA Calculator using Reactjs Preview imagePrerequisites and Technologies:
Approach:
Utilizes ReactJS functional components and state managements to create an interactive web-based GPA Calculator. This application begins by capturing the input course details including course name, credits and earned grades and add them into a dynamic list which is visible to the user and user can also delete the individual entry for that list. The GPA is continuously updated and displayed on the interface with precision up to two decimal places.
Steps to create the application:
Step 1: Set up React project using the command
npx create-react-app <<name of project>>
Step 2: Navigate to the project folder using
cd <<Name_of_project>>
Step 3: Create a folder “components” and add four new files in it and name them as CourseForm.js, and CourseList.js, GPACalculator.js and GPACalculator.css
Project Structure:
Project StructureThe updated dependencies in package.json will look like this:
{
"name": "GPACalculator",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
}
Example: Write the following code in respective files
- App.js: This file imports the GPACalculator components and exports it.
- GPACalculator.js: This file is the main component of a GPA calculator application built with React. It manages the state for course data and rendering of the user interface.
- CourseForm.js: This file defines a React component responsible for rendering and handling user input for adding new courses to the GPA calculator. It includes fields for course name, credit hours, and grade selection.
- CourseList.js: This file contains a React component responsible for displaying the list of added courses and calculating the GPA based on the entered grades and credit hours in the GPA calculator application.
- GPACalculator.css: This file contains the design of the GPACalculator elements.
CSS
/* GPACalculator.css*/
@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+Mono&display=swap');
*{
box-sizing: border-box;
font-family: 'Noto Sans Mono', monospace;
}
body{
padding: 0;
margin: 0;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background-color: #f1f6f6;
}
.container {
max-width: 650px;
margin: 5px;
width: calc(100% - 10px);
}
.container h1{
margin: 0;
margin-bottom: 10px;
text-align: center;
font-size: 25px;
}
.section{
border: 1px solid #ced4da;
border-radius: 5px;
padding: 20px;
border: 1px solid #ced4da;
background: #fff;
box-shadow: 0 0 6px rgba(0,0,0,0.25);
}
.section1{
display: flex;
justify-content: space-between;
align-items: center;
width: 100%;
}
.section1 div{
margin: 5px;
}
.section1 div:first-child input{
max-width: 150px;
text-align: left;
}
.section1 select{
width: 100%;
font-size: 1rem;
padding: 8px 4px;
font-weight: 400;
line-height: 1.5;
color: #495057;
outline: none;
background-color: #fff;
background-clip: padding-box;
border: 1px solid #ced4da;
border-radius: 0.25rem;
}
.section1 div:nth-child(2) input{
max-width: 90px;
text-align: left;
}
.section1 div:nth-child(3){
width: 50px;
text-align: left;
}
.section1 div:nth-child(4){
width: 60px;
text-align: left;
}
.section1 p{
margin: 5px 5px 5px 0px;
text-align: left;
font-size: 14px;
}
input{
width : 100%;
font-size: 1rem;
padding: 6px 10px;
font-weight: 400;
line-height: 1.5;
color: #495057;
outline: none;
background-color: #fff;
background-clip: padding-box;
border: 1px solid #ced4da;
border-radius: 0.25rem;
}
.section button{
padding: 9.5px;
outline: none;
background-color: #fff;
color: #1d9bf0;
border: 1px solid #1d9bf0;
border-radius: 4px;
cursor: pointer;
font-weight: bold;
transition: 0.5s all;
}
.section button:hover{
color: white;
background-color: #1d9bf0;
border-color: #1d9bf0;
border-width: 1px;
}
.section2 ul{
font-size: 14px;
list-style-type: none;
padding-inline-start: 0px;
display: grid;
align-items: center;
margin: 5px;
grid-template-columns: 1fr 1fr 1fr 1fr;
text-align: center;
}
JavaScript
// App.js
import './App.css';
import GPACalculator from './components/GPACalculator';
function App() {
return (
<div className="App">
<GPACalculator />
</div>
);
}
export default App;
JavaScript
// GPACalculator.js
import React, { useState } from 'react';
import './GPACalculator.css';
import CourseForm from './CourseForm';
import CourseList from './CourseList';
const gradePoints = {
'A+': 4.0,
'A': 4.0,
'A-': 3.7,
'B+': 3.3,
'B': 3.0,
'B-': 2.7,
'C+': 2.3,
'C': 2.0,
'C-': 1.7,
'D+': 1.3,
'D': 1.0,
'D-': 0.7
};
const GPACalculator = () => {
const [courses, setCourses] = useState([]);
const handleAddCourse = (newCourse) => {
setCourses([...courses, newCourse]);
};
const handleDeleteCourse = (index) => {
const updatedCourses = courses.filter((course, i) => i !== index);
setCourses(updatedCourses);
};
const calculateGPA = () => {
let totalGradePoints = 0;
let totalCreditHours = 0;
courses.forEach((course) => {
totalGradePoints += gradePoints[course.grade] * course.creditHours;
totalCreditHours += course.creditHours;
});
return totalCreditHours === 0 ? 0 : totalGradePoints / totalCreditHours;
};
return (
<div className='container'>
<h1>GPA Calculator</h1>
<div className="section">
<CourseForm onAddCourse={handleAddCourse} />
<CourseList courses={courses} onDeleteCourse={handleDeleteCourse} calculateGPA={calculateGPA} />
</div>
</div>
);
};
export default GPACalculator;
JavaScript
// CourseForm.js
import React, { useState } from 'react';
const CourseForm = ({ onAddCourse }) => {
const [courseName, setCourseName] = useState('');
const [creditHours, setCreditHours] = useState(0);
const [grade, setGrade] = useState('A+');
const handleAddCourse = () => {
if (courseName && creditHours > 0 && grade) {
const newCourse = {
courseName,
creditHours,
grade,
};
onAddCourse(newCourse);
setCourseName('');
setCreditHours(0);
setGrade('A+');
} else {
alert('Please enter valid course details.');
}
};
return (
<div className="section1">
<div>
<p>Course</p>
<input
type="text"
value={courseName}
onChange={(e) => setCourseName(e.target.value)}
/>
</div>
<div>
<p>Credits</p>
<input
type="number"
value={creditHours}
onChange={(e) => setCreditHours(Number(e.target.value))}
/>
</div>
<div>
<p>Grade</p>
<select value={grade} onChange={(e) => setGrade(e.target.value)}>
<option value="A+">A+</option>
<option value="A">A</option>
<option value="A-">A-</option>
<option value="B+">B+</option>
<option value="B">B</option>
<option value="B-">B-</option>
<option value="C+">C+</option>
<option value="C">C</option>
<option value="C-">C-</option>
<option value="D+">D+</option>
<option value="D">D</option>
<option value="D-">D-</option>
</select>
</div>
<div>
<p style={{ opacity: 0 }}>-</p>
<button onClick={handleAddCourse}>Add</button>
</div>
</div>
);
};
export default CourseForm;
JavaScript
// CourseList.js
import React from 'react';
const CourseList = ({ courses, onDeleteCourse, calculateGPA }) => {
return (
<div className="section2">
<div>
<h2>Course List</h2>
<ul style={{ borderBottom: '1px solid #ced4da', paddingBottom: '10px' }}>
<li>Course</li>
<li>Credits</li>
<li>Grade</li>
<li>Action</li>
</ul>
{courses.map((course, index) => (
<ul key={index}>
<li>{course.courseName}</li>
<li>{course.creditHours}</li>
<li>{course.grade}</li>
<li><button onClick={() => onDeleteCourse(index)}>Delete</button></li>
</ul>
))}
</div>
<div>
<h3>GPA: {calculateGPA().toFixed(2)}</h3>
</div>
</div>
);
};
export default CourseList;
Steps to run the application:
Step 1: Type the following command in terminal.
npm start
Step 2: Open web-browser and type the following URL
http://localhost:3000/
Output:
GPA Calculator using React
Similar Reads
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
React Fundamentals
React IntroductionReactJS is a component-based JavaScript library used to build dynamic and interactive user interfaces. It simplifies the creation of single-page applications (SPAs) with a focus on performance and maintainability.React.jsWhy Use React?Before React, web development faced issues like slow DOM updates
7 min read
React Environment SetupTo run any React application, we need to first setup a ReactJS Development Environment. In this article, we will show you a step-by-step guide to installing and configuring a working React development environment.Pre-requisite:We must have Nodejs installed on our PC. So, the very first step will be
3 min read
React JS ReactDOMReactDOM is a core React package that provides methods to interact with the Document Object Model, or DOM. This package allows developers to access and modify the DOM. It is a package in React that provides DOM-specific methods that can be used at the top level of a web app to enable an efficient wa
3 min read
React JSXJSX stands for JavaScript XML, and it is a special syntax used in React to simplify building user interfaces. JSX allows you to write HTML-like code directly inside JavaScript, enabling you to create UI components more efficiently. Although JSX looks like regular HTML, itâs actually a syntax extensi
5 min read
ReactJS Rendering ElementsIn this article we will learn about rendering elements in ReactJS, updating the rendered elements and will also discuss about how efficiently the elements are rendered.What are React Elements?React elements are the smallest building blocks of a React application. They are different from DOM elements
3 min read
React ListsReact Lists are used to display a collection of similar data items like an array of objects and menu items. It allows us to dynamically render the array elements and display repetitive data.Rendering List in ReactTo render a list in React, we will use the JavaScript array map() function. We will ite
5 min read
React FormsForms are an essential part of any application used for collecting user data, processing payments, or handling authentication. React Forms are the components used to collect and manage the user inputs. These components include the input elements like text field, check box, date input, dropdowns etc.
5 min read
ReactJS KeysA key serves as a unique identifier in React, helping to track which items in a list have changed, been updated, or removed. It is particularly useful when dynamically creating components or when users modify the list. In this article, we'll explore ReactJS keys, understand their importance, how the
5 min read
Components in React
React ComponentsIn React, React components are independent, reusable building blocks in a React application that define what gets displayed on the UI. They accept inputs called props and return React elements describing the UI.In this article, we will explore the basics of React components, props, state, and render
4 min read
ReactJS Functional ComponentsIn ReactJS, functional components are a core part of building user interfaces. They are simple, lightweight, and powerful tools for rendering UI and handling logic. Functional components can accept props as input and return JSX that describes what the component should render.Stateless (before hooks)
5 min read
React Class ComponentsClass components are ES6 classes that extend React.Component. They allow state management and lifecycle methods for complex UI logic.Used for stateful components before Hooks.Support lifecycle methods for mounting, updating, and unmounting.The render() method in React class components returns JSX el
4 min read
ReactJS Pure ComponentsReactJS Pure Components are similar to regular class components but with a key optimization. They skip re-renders when the props and state remain the same. While class components are still supported in React, it's generally recommended to use functional components with hooks in new code for better p
4 min read
ReactJS Container and Presentational Pattern in ComponentsIn this article we will categorise the react components in two types depending on the pattern in which they are written in application and will learn briefly about these two categories. We will also discuss about alternatives to this pattern. Presentational and Container ComponentsThe type of compon
2 min read
ReactJS PropTypesIn ReactJS PropTypes are the property that is mainly shared between the parent components to the child components. It is used to solve the type validation problem. Since in the latest version of the React 19, PropeTypes has been removed. What is ReactJS PropTypes?PropTypes is a tool in React that he
5 min read
React Lifecycle In React, the lifecycle refers to the various stages a component goes through. These stages allow developers to run specific code at key moments, such as when the component is created, updated, or removed. By understanding the React lifecycle, you can better manage resources, side effects, and perfo
7 min read
React Hooks
Routing in React
Advanced React Concepts
React Projects