方便我們計算學分績點的JavaScript

來源:互聯網
上載者:User

基於目前我們學校教務處的管理系統, 依靠Javascript的幫忙, 我們可以很方便地計算成績.

測試用HTML:

<table class="datagridstyle" cellspacing="0" cellpadding="3" border="0" id="DataGrid1"
        style="width: 100%; border-collapse: collapse;">
        <tr class="datagridhead">
            <td>
                課程代碼</td>
            <td>
                課程名稱</td>
            <td>
                課程性質</td>
            <td>
                成績</td>
            <td>
                補考成績</td>
            <td>
                重修成績</td>
            <td>
                學分</td>
            <td>
                績點</td>
            <td>
                輔修標記</td>
        </tr>
        <tr class="datagrid1212">
            <td>
                24109505</td>
            <td>
                資料庫系統實驗</td>
            <td>
                專業基礎課</td>
            <td>
                優秀</td>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
            <td>
                0.5</td>
            <td>
                4.50</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td>
                24100320</td>
            <td>
                單片機原理及應用</td>
            <td>
                專業基礎選修課</td>
            <td>
                78</td>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
            <td>
                2.0</td>
            <td>
                2.80</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr class="datagrid1212">
            <td>
                24100915</td>
            <td>
                UML</td>
            <td>
                校選修課</td>
            <td>
                63</td>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
            <td>
                1.5</td>
            <td>
                1.30</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td>
                24200510</td>
            <td>
                資料庫系統課程設計</td>
            <td>
                設計(論文)</td>
            <td>
                優秀</td>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
            <td>
                1.0</td>
            <td>
                4.50</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr class="datagrid1212">
            <td>
                24203910</td>
            <td>
                可程式化序控制器與工業控制網路課程設計</td>
            <td>
                設計(論文)</td>
            <td>
                良好</td>
            <td>
                &nbsp;</td>
            <td>
                &nbsp;</td>
            <td>
                1.0</td>
            <td>
                3</td>
            <td>
                &nbsp;</td>
        </tr>
    </table> 

JavaScript:

<script type="text/javascript" language="javascript">
    //一學年的平均學分績點=該學年修全部課程的學分績點之和÷所修課程的總學分(校選修不計算在內)
    //學業成績平均分=一學年平均學分績點摺合為百分制的得數
    //考通過的課程,按實際成績記分,並給予學分,但其績點均為“0”。重修的課程在該年度中按不及格科目計算,績點為0。
    function Calculate() {   
        var table = document.getElementById("DataGrid1");   
        var totalMark = 0;   
        var failCount = 0;   
        var subjectCount = 0;
        var totalRate = 0;   
        var totalRawRate = 0;   
        for(var i = 1; i < table.rows.length; i++) {       
            if(table.rows[i].cells[2].innerHTML == "校選修課") continue;   
            subjectCount++;   
            var mark = getMark(table.rows[i].cells[3].innerHTML);       
            if(mark < 60) {           
                failCount++;       
            }       
            totalMark += mark;       
            totalRate += getMark(table.rows[i].cells[7].innerHTML)
                * getMark(table.rows[i].cells[6].innerHTML);       
            totalRawRate += getMark(table.rows[i].cells[6].innerHTML);   
        }   
        var resultRow = document.getElementById("resultRow");   
        if(!resultRow) {       
            resultRow = document.createElement("tr");       
            resultRow.id = "resultRow";   
        }   
        resultRow.innerHTML = "";   
        var results = ["總分:", totalMark, "平均分:", (totalMark/subjectCount).toFixed(3),
            "平均學分績點:", (totalRate/totalRawRate).toFixed(3), "不及格數:",failCount];   
        for(var i = 0; i < results.length; i++) {       
            var td = document.createElement("td");       
            td.innerHTML = results[i];       
            resultRow.appendChild(td);   
        }   
        table.lastChild.appendChild(resultRow);
    }
    function getMark(markStr) {   
        switch(markStr) {       
            case "優秀": return 95;       
            case "良好": return 85;       
            case "中等": return 75;       
            case "及格": return 65;       
            case "不及格":       
            case "" :        
            case "&nbsp;": return 0;       
            default: return parseFloat(markStr);   
        }
    }
    Calculate();
   </script>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.