Creating a Dynamic System for Converting Numeric Digits to Alphabetical Grades

Utilizing the code provided below, you can calculate a user's grade point average ranging from A+ to E-. Is there a way, within the following fiddle, to not only display the GPA but also convert it into a corresponding letter grade from A+ to E-? Currently, users input their grades in the range of A+ to E-, and the system calculates the GPA accordingly.

If a new fiddle with this additional feature could be shared, I would greatly appreciate it as I am still learning how to code.

Fiddle Link

Thank You

HTML:

<button class="button" data-bind="click: addClass">Add a New Class</button>
<button class="button">
Apply
</button>
<hr>

<ul align="center" data-bind="foreach: classes">
    <li>
        <label>Subject:</label><input type="text" data-bind="value: title" placeholder="E.g: English"/>
        <select disabled data-bind="value: credits">
        <option  selected data-bind="value: credits">1</option>
        </select>
        <label>Grade:</label>
        <input type="text" data-bind="value: letterGrade" placeholder="E.g: A+"/>
        <br>
        <br>
    </li>
</ul>



<hr />

<br>
<h4>
Your GPA is: <b><span data-bind="text: totalGPA"></span></b>
</h4>
<br>
<h4>
Final Grade: <span><i>(Where Users Grade is Calculated from A+ to E-)</i></span>
</h4>

JQuery:

function Class(title, credits, letterGrade) {
    var self = this;
    var gradeMapping = {
        'A+': 15,
        'A': 14,
        'A-': 13,
        'B+': 12,
        'B': 11,
        'B-': 10,
        'C+': 9,
        'C': 8,
        'C-': 7,
        'D+': 6,
        'D': 5,
        'D-': 4,
        'E+': 3,
        'E': 2,
        'E-': 1
    }
    self.title = ko.observable(title);
    self.credits = ko.observable(credits);
    self.letterGrade = ko.observable(letterGrade);
    self.gpa = ko.computed(function() {
        return gradeMapping[self.letterGrade()];
    });
}

function GpaCalcViewModel() {
    var self = this;
    self.classes = ko.observableArray();
    self.totalGPA = ko.computed(function() {
        var totalWeightedGPA = 0,
            totalCredits = 0;
        $.each(self.classes(), function() {
            totalWeightedGPA += (this.gpa() * this.credits());
            totalCredits += (this.credits() * 1);
        })
        return totalWeightedGPA / totalCredits;
    });
    self.addClass = function() {
        self.classes.push(new Class());
    }
};
var viewModel = new GpaCalcViewModel();
ko.applyBindings(viewModel);

CSS:

@import url(http://fonts.googleapis.com/css?family=Open+Sans:400,700);
@import url(http://fonts.googleapis.com/css?family=Inconsolata:400);
* { text-rendering: optimizelegibility;}
body, input, textarea, select, button { font-family: 'Open Sans', sans-serif; }
pre { font-family: 'Inconsolata', monospace; }

span {font-size: 18px;}


h1 {font-size: 25px;}

Answer №1

To start, in order to retrieve the desired value, you must data-bind it using data-bind="text: totalGrade".

Next, ensure that the gradeMapping array is moved outside the class function so that it can be accessed globally.

Lastly, create a function that maps GPA values back to corresponding grades.

var gradeMapping = {
        'A+': 15,
        'A': 14,
        'A-': 13,
        'B+': 12,
        'B': 11,
        'B-': 10,
        'C+': 9,
        'C': 8,
        'C-': 7,
        'D+': 6,
        'D': 5,
        'D-': 4,
        'E+': 3,
        'E': 2,
        'E-': 1
    }
function Class(title, credits, letterGrade) {
    var self = this;
    self.title = ko.observable(title);
    self.credits = ko.observable(credits);
    self.letterGrade = ko.observable(letterGrade);
    self.gpa = ko.computed(function() {
        return gradeMapping[self.letterGrade()];
    });
}

function GpaCalcViewModel() {
    var self = this;
    self.classes = ko.observableArray();
    self.totalGPA = ko.computed(function() {
        var totalWeightedGPA = 0,
            totalCredits = 0;
        $.each(self.classes(), function() {
            totalWeightedGPA += (this.gpa() * this.credits());
            totalCredits += (this.credits() * 1);
        })
        return totalWeightedGPA / totalCredits;
    });
    self.totalGrade = ko.computed(function() {
        var totalWeightedGPA = 0,
            totalCredits = 0;
        var gradeLetter = null;
        $.each(self.classes(), function() {
            totalWeightedGPA += (this.gpa() * this.credits());
            totalCredits += (this.credits() * 1);
        });
        $.each(gradeMapping, function(letter, number) {
        if(number == Math.round(totalWeightedGPA / totalCredits)){
            gradeLetter = letter;
            }
        })
        return gradeLetter;
    });
    self.addClass = function() {
        self.classes.push(new Class());
    }
};
var viewModel = new GpaCalcViewModel();
ko.applyBindings(viewModel);
@import url(http://fonts.googleapis.com/css?family=Open+Sans:400,700);
@import url(http://fonts.googleapis.com/css?family=Inconsolata:400);
* { text-rendering: optimizelegibility;}
body, input, textarea, select, button { font-family: 'Open Sans', sans-serif; }
pre { font-family: 'Inconsolata', monospace; }

span {font-size: 18px;}


h1 {font-size: 25px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/2.2.1/knockout-min.js"></script>
<button class="button" data-bind="click: addClass">Add a New Class</button>
<button class="button">
Apply
</button>
<hr>

<ul align="center" data-bind="foreach: classes">
    <li>
        <label>Subject:</label><input type="text" data-bind="value: title" placeholder="E.g: English"/>
        <select disabled data-bind="value: credits">
        <option  selected data-bind="value: credits">1</option>
        </select>
        <label>Grade:</label>
        <input type="text" data-bind="value: letterGrade" placeholder="E.g: A+"/>
        <br>
        <br>
    </li>
</ul>



<hr />

<br>
<h4>
Your GPA is: <b><span data-bind="text: totalGPA"></span></b>
</h4>
<br>
<h4>
Final Grade: <span data-bind="text: totalGrade"></span>
</h4>

Check out this example on jsFiddle for more details: jsFiddle

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Transfer information between two devices remotely through AJAX

I am in the process of developing a web application that utilizes a mobile phone as a controller, similar to this example: . The concept is quite simple - I just need to transfer text entered on the phone to the computer. There is no need for a database, ...

How to dynamically insert a hyperlink inside a <td> element within a table using JavaScript

What is the best way to create a hyperlink in a <td> within a dynamic table? I want the first <td> to be a link that combines a URL with the cell value. This is how the dynamic table is created: for (var i = 0; i < riskData.length; i++) { ...

Error: Trying to set a value to an undefined object in the bind() method

I am currently working on implementing a listener for my video player in order to update the input range on some custom controls. However, I have encountered an issue with an error message that states Uncaught ReferenceError: Invalid left-hand side in as ...

The functionality of Jquery datatables seems to be faulty when navigating to the second page

While utilizing the jQuery datatables plugin, I encountered an issue where the event click function only worked on the first page and not on subsequent pages. To address this problem, I discovered a helpful resource at https://datatables.net/faqs/ Q. My ...

What are some ways to personalize a scrollbar?

I am looking to customize the scrollbar within a div. I attempted to modify it using the code below, but I encountered difficulties when trying to change the scroll buttons and did not achieve my desired outcome. Additionally, this customization did not wo ...

Playing embedded YouTube videos automatically in Safari 11 without user interaction

I’m encountering an issue with a simple modal dialog: When a user clicks on a button, the modal overlay appears. An embedded YouTube <iframe> is then added. Everything works smoothly in most browsers, except for Safari 11.1. Safari’s new auto ...

What is the process for retrieving data from mongoDB and displaying it by year in a single row?

I have received an array of data from MongoDB, which includes id, userName, and date. The date consists of years and months. My goal is to display this data categorized by years. How can I construct a query to the database that will show the data for all y ...

hitting the value of the text input

Is there a way to strike through only the first word in an input box of type text, without editing the html? I've tried using css text-decoration: line-through; but it's striking both words. Any suggestions on how to achieve this using javascript ...

For each error that occurs when there is only one item in the array during a post request

My nodejs server has a JSON schema like this: var UserSchema = new Schema({ nick: String, deviceId: String, visivel: Boolean, checks: [{date: {type:String},log: {type:Number},lng: {type:Number}}] }); In the Post code ...

There seems to be a contradiction in my code - I am returning a Promise but TypeScript is throwing an error saying that the

I currently have a function that retrieves a bot's inventory on the Frontend fetchBotInventory() { this.socket.emit('fetch bot inv'); this.socket.on('bot inv', (botInventory) => { return new Promise((resolve, re ...

Reduce the height of the navigation bar

I used the header example from Bootstrap's documents here: https://getbootstrap.com/docs/5.3/examples/headers/# The code I have is: <div class="container fixed-top bg-white"> <header class="d-flex flex-wrap justify-conte ...

Is there a regular expression that can be used to extract sentences from HTML

As I delve into the world of HTML parsing, I'm starting to understand that using regular expressions for this task is not always the best approach. However, in my Perl class, I am currently experimenting with utilizing regular expressions to extract a ...

Are you facing difficulties while trying to utilize useContext in your React application?

I have a basic React application where I need to implement useContext. (by the way, I am using Vite + React) Below is the code for my Context.jsx file: import React, {useContext} from 'react'; const emailContext = React.createContext(); expor ...

Having difficulty populating the token in the h-captcha-response innerHTML and g-recaptcha-response innerHTML

I am attempting to use 2captcha along with Selenium and Python to bypass an Hcaptcha. After receiving my 2captcha token, I attempt to input it into the textareas labeled 'h-captcha-response' and 'g-captcha-response'. However, this app ...

I've been struggling with my Create React app for the past two days, and it just won

When trying to create a React project using the command `npx create-react-app reactproject`, I encountered an error: npm ERR! code ENOENT npm ERR! syscall spawn C:\Users\SUJITKUMAR\Desktop npm ERR! path D:\WebDev\React npm ERR! ...

The div swiftly clicks and moves beyond the screen with animated motion

Here is a code snippet that I am working with: $(document).ready(function(){ $(".subscriptions").click(function (){ if($(".pollSlider").css("margin-right") == "-200px"){ $('.pollSlider').animate({"margin-right": '+ ...

Implementing Node.JS ajax to update current JSON information

I am seeking assistance in updating data within a JSON file using NODE.JS. Currently, my method adds the data with the same ID as expected. However, upon receiving the data back, it eliminates the last duplicate because it encounters the old value first. I ...

Simply click on a single checkbox in ReactJS

Is there a way to implement a method in which clicking on one checkbox will automatically deselect the other checkboxes, allowing only one selection at a time? import React, { Component } from 'react'; export default class Tablerow extends Comp ...

Discover captivating visuals that effortlessly occupy the entire breadth, while gracefully encompassing a mere quarter of the total vertical space on the

It's become quite challenging for me to locate images that can occupy the entire width of a page while maintaining just 25% of the page height. Whenever I make adjustments to the image's CSS, it ends up appearing distorted because its width is di ...

The Angular $digest cycle ensures that the entire view is refreshed, rather than just a portion

Encountering a peculiar problem in Angular 1 where the script is getting stuck in an endless loop, eventually causing the browser to freeze. Here's what I'm attempting: <script> $scope.A = true; $scope.B = [{blah},{blah}]; $sc ...