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.
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;}