整数是追加而不是添加(Javascript)

时间:2014-02-06 20:47:02

标签: javascript integer expression

有一些问题在做一个非常简单的包含JavaScript项目。

WeightedGPA附加了值,而不是在使用+ =

时添加它

Caluclation我想做的是:加权平均GPA:尝试的小时数乘以每个课程的成绩点,然后除以总的尝试小时数

感谢任何帮助。已附上完整代码

    <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>

3 个答案:

答案 0 :(得分:1)

你想通过使用parseintparsefloat函数强制将所有内容视为一个数字 - 因为它是,当JS怀疑某个字符串是一个字符串时,+将意味着串联。

在我看来,你正在做一个奇怪的总和 - 你“添加”的第一件事是名字,这可能是一个字符串。为什么要形成这个总和(而不是两个单独变量中的等级和信用小时总和,所以你可以计算你声称要做的加权平均值。)

也许您应该考虑将不同的字段累积到一个数组集合中 - 当您汇总了所有数据时,您可以计算小时数组的总和以及(小时*等级)的总和,这样您就可以得到加权平均值。

你还需要更多的东西来重新开始吗?

答案 1 :(得分:0)

在宣布加权的Gpa之后你错过了一个分号,这只是一个错字吗?它可能搞砸了宣言。

答案 2 :(得分:0)

更改你的行'var courseData =“”;'到'var courseData = 0;'这应该可以解决问题。

声明使用“”告诉javascript courseData是一个字符串,所以你的+ =操作将被视为连接而不是数学函数。