Gfraser
Gfraser

Reputation: 15

Integers are appending instead of being added(Javascript)

Having some issues doing a pretty simple contained JavaScript project.

WeightedGPA is having values appended to it instead of adding it when using +=

Caluclation I am trying to do is: Weighted Avg GPA: Attempted hours multiplied by grade point for each course in the term, then divide by the total attempted hours

Any help is appreciated. Full code is attached

    <html>
<head>
</head>
<body>

<script type="text/javascript">
var courseValues = new Array();
var x = 0;
var debug = true;

function courseObjCreate()
{
var courseData = "";
courseData += document.getElementById("name").value;
courseData += ","
courseData += document.getElementById("number").value;
courseData += ","
courseData += document.getElementById("mark").value;
courseData += ","
courseData += document.getElementById("hours").value;
courseData += ","
courseData += document.getElementById("credits").value;
courseValues[x] = courseData;

alert("Course added!");

var elemName = document.getElementById("name");
elemName.value = "";
var elemNumber = document.getElementById("number");
elemNumber.value = "";
var elemMark = document.getElementById("mark");
elemMark.value = "";
var elemHours = document.getElementById("hours");
elemHours.value = "";
var elemCredits = document.getElementById("credits");
elemCredits.value = "";

x++;


}

</script>
<center>
<h1> Student transcript</h1>
<br>
<h2> Enter course details</h2>
<form>
  Course name:      <input id="name" type="text" /><br>
  Course number:    <input id="number" type="text" /><br>
  Course mark:      <input id="mark" type="text" /><br>
  Course hours:     <input id="hours" type="text" /><br>
  Course credits:   <input id="credits" type="text" /><br>
  <input type="button" value="Submit" onClick ="courseObjCreate()"> Click me to add another course
<br>  
  <input type="button" value="Generate" onClick ="tblCall()">  Click me to generate the course table
  <br>
</form>
<br>
<br>


<script type="text/javascript">
function createTable()
{

    if(debug) console.log("Create Table Start");
    var gpaSum = (0.0);
    var hoursSum = 0;
    var credits = 0;
    var creditsSum = 0;
    var gpaSum = (0.0)
    var prnStrF = "";
    var weightedGpa = (0.0)


    if(debug) console.log("weightedGpa: " + weightedGpa);

    prnStrF += '<table border="1" cellspacing="1" cellpadding="5">';
    prnStrF += '<tr><td>Course name</td><td>Couse number</td><td>Course mark(percentage)</td><td>Course letter grade</td><td>Course Grade Point</td><td>Course hours</td><td>Course credits</td>';

    for (var i = 0; i < courseValues.length; i++) 
    {

        courseLetter ='';
        courseGPA= (0.0).toFixed(2);

        var coursePrintStr = courseValues[i].toString();
        var prn = coursePrintStr.split(",")

        if(debug) console.log("prn[2]: " + prn[2]);
        var markNum = parseInt(prn[2]);
        if(debug) console.log("MarkNum: " + markNum);

        switch(true)
        {
            case (markNum < 50):
                if(debug) console.log("less than 50");
                courseLetter = 'F';
                courseGPA = 0.0;
                credits = 0;
                weightedGpa += 0;
                break;

            case (markNum > 49 && markNum  < 55):
                if(debug) console.log("more than 49 less than 55");
                courseLetter = 'D';
                courseGPA = 1.0;
                credits = parseInt(prn[4]);
            weightedGpa += parseInt(courseGPA * parseInt(prn[3]));
                break;

            case (markNum > 54 && markNum < 60):
            courseLetter = 'D+';
            courseGPA = 1.3;
            credits = parseInt(prn[4]);
            weightedGpa += parseInt(courseGPA * parseInt(prn[3]));
            break;

            case (markNum > 59 && markNum < 63):
            courseLetter = 'C-';
            courseGPA = 1.7;
            credits = parseInt(prn[4]);
            weightedGpa += parseInt(courseGPA * parseInt(prn[3]));
            break;

            case (markNum > 62 && markNum < 67):
            courseLetter = 'C';
            courseGPA = 2.0;
            credits = parseInt(prn[4]);
            weightedGpa += parseInt(courseGPA * parseInt(prn[3]));
            break;

            case (markNum > 66 && markNum < 70):
            courseLetter = 'C+';
            courseGPA = 2.3;
            credits = parseInt(prn[4]);
            weightedGpa += parseInt(courseGPA * parseInt(prn[3]));
            break;

            case (markNum > 69 && markNum < 73):
            courseLetter = 'B-';
            courseGPA = 2.7;
            credits = parseInt(prn[4]);
            weightedGpa += parseInt(courseGPA * parseInt(prn[3]));
            break;

            case (markNum > 72 && markNum < 77):
            courseLetter = 'B';
            courseGPA = 3.0;
            credits = parseInt(prn[4]);
            weightedGpa += parseInt(courseGPA * parseInt(prn[3]));
            break;

            case (markNum > 76 && markNum < 80):
            courseLetter = 'B+';
            courseGPA = 3.3;
            credits = parseInt(prn[4]);
            weightedGpa += parseInt(courseGPA * parseInt(prn[3]));
            break;

            case (markNum > 79 && markNum < 85):
            if(debug) console.log("MarkNumInCase: " + markNum);
            courseLetter = 'A-';
            courseGPA = 3.7;
            credits = parseInt(prn[4]);
            weightedGpa += parseInt(courseGPA * parseInt(prn[3]));
            break;

            case (markNum > 84):
                courseLetter = 'A';
                courseGPA = 4.0;
                credits = parseInt(prn[4]);
            weightedGpa += parseInt(courseGPA * parseInt(prn[3]));
                break;

            default:
                alert("Something broke in the switch case. Probably a String input instead of an int");

        }
    courseGPA = courseGPA.toFixed(2);
    if(debug) console.log("gpaSum: " + gpaSum);
    creditsSum += credits;
    if(debug) console.log("creditsSum: " + creditsSum);
    hoursSum += parseInt(prn[3]);
    if(debug) console.log("hoursSum: " + hoursSum);
    if(debug) console.log("weightedGpa: " + weightedGpa);
    weightedGpa = (weightedGpa/hoursSum).toFixed(2);
    if(debug) console.log(prnStrF);
    prnStrF +='<tr>';
    prnStrF += '<td>' + String(prn[0]) + '</td>' + '<td>' + String(prn[1]) + '</td>' + '<td>' + String(prn[2]) + "%" + '</td>'+ '<td>'+ String(courseLetter) + '</td>' + '<td>' + String(courseGPA) +'</td>' +'<td>' + String(prn[3]) + '</td>' +'<td>' + String(prn[4]) + '</td>';

    prnStrF += '</tr>';

    }

    prnStrF += '</table>';
    if(debug) console.log("hoursSum: " + hoursSum);
    if(debug) console.log("creditsSum: " + creditsSum);
    if(debug) console.log("weightedGpa: " + weightedGpa);


    prnStrF += '<br><br>';
    prnStrF +='<table border="1" cellspacing="1" cellpadding="5">';
    prnStrF += '<tr><td><b>Report</td><td></td><td>Attemped hours</td><td>Earned credits </td><td>Weighted Average GPA</td></b></tr>';
    prnStrF += '<tr><td><b>Report</td><td></td><td>'+ String(hoursSum) + '</td><td>' + String(creditsSum) +' </td><td>'+ String(weightedGpa)+'</b></td></tr>';
    prnStrF += '</table>';
    if(debug) console.log(prnStrF);
    return prnStrF;
}

</script>

<script type="text/javascript">
function tblCall()
{

    document.getElementById('generateHere').innerHTML = createTable();

}
</script>
<br>
<br>

<div id='generateHere'></div>
</center>
</body>
</html>

Upvotes: 0

Views: 88

Answers (3)

Floris
Floris

Reputation: 46375

You want to force everything to be considered a number by using the parseint or parsefloat functions - as it is, the moment JS suspects something is a string, the + will mean concatenation.

It seems to me that you are making a strange sum - the first thing you "add" is the name, which is likely to be a string. Why are you forming this sum (instead of the sum of grades and sum of credit hours in two separate variables, so you can compute the weighted average which is what you claim you want to do).

Perhaps you should consider acumulating the different fields into a collection of arrays - and when you have all the data assembled, you compute the sum of the hours array, and the sum of (hours * grades), so you can get the weighted average.

Do you need more than this to get going again?

Upvotes: 1

J Ellis
J Ellis

Reputation: 678

Change your line 'var courseData = "";' to 'var courseData = 0;' and that should do the trick.

Declaring using "" tells javascript that courseData is a string, so your += operations will be taken as concatenations instead of mathematical functions.

Upvotes: 0

ltalhouarne
ltalhouarne

Reputation: 4636

You are missing a semi-colon after declaring weightedGpa, is that just a typo? It might mess up the declaration.

Upvotes: 0

Related Questions