用JS制作一个简易GPA计算器

这是我第一次使用JS,有问题的地方还希望大佬指出
程序的容错性还没做到完美,日后补充


<html>

<head>
    <meta charset="utf-8">
    <title>GPA计算工具title>
    <script>
        function getNum() {
            document.getElementById("in").innerHTML="";
            var cnum = Number(document.getElementById("cNum").value);
            var content;
            for (i = 0; i < cnum; i++) {
                content = document.getElementById("res").innerHTML;
                document.getElementById("in").innerHTML = content + "
第"
+ (i + 1) + "门成绩: + i + "\" type=\"number\"> 学分: + i + "\" type=\"number\">"; } content = document.getElementById("res").innerHTML; document.getElementById("in").innerHTML = content + "
"
; } function getcr(x) { if (x < 60) return Number(0.0); else if (x <= 63) return Number(1.0); else if (x <= 67) return Number(1.5); else if (x <= 71) return Number(2.0); else if (x <= 74) return Number(2.3); else if (x <= 77) return Number(2.7); else if (x <= 81) return Number(3.0); else if (x <= 84) return Number(3.3); else if (x <= 89) return Number(3.7); else return Number(4.0); } function deal() { var score = []; var credit = []; var cnum = Number(document.getElementById("cNum").value); for (i = 0; i < cnum; i++) { score.push(Number(document.getElementById("sc" + i).value)); credit.push(Number(document.getElementById("cr" + i).value)); } var total = 0.0; for (i = 0; i < cnum; i++) { total = total + credit[i]; } var res = 0.0; for (i = 0; i < cnum; i++) { res = res + (credit[i] / total) * getcr(score[i]); } if(isNaN(res) || res<0) alert("抱歉,发生了错误!\n请检查有无漏项或者输入的某个学分为零或负数\n请修正后重新点击计算按钮") else alert("您的GPA为" + res + "\n您可以点击刷新进行第二次使用!"); }
script> head> <body> 课程数:<br> <input id="cNum" type="number"><br> <button onclick="getNum()">下一步button> <div id="res">div><br><br><br> body> html>

你可能感兴趣的:(学习)