不等式未显示正确的输出,JS

时间:2018-07-04 22:25:14

标签: javascript inequality

我正在尝试制作一个简单的最终成绩计算器,并且不等式的输出存在一些问题。它由4个等级组成,每个等级都有一个双值。然后,将他们乘以他们应有的权重,并放入一系列不等式以查看参与者将获得的成绩。不幸的是,即使double值正确,我也得到了不正确的字母值。我认为这是我的不平等,但我无法弄清楚它们有什么问题。任何帮助将不胜感激!

我不想包含所有代码,因为它很多,但是它们都对应于这样的单个下拉菜单。

<form name="GPA">
            <select name="demoSelect" onchange="showData2()">
        <option value="zilch">Select Quarter 1:</option>
        <option id="4.0" >A</option>
        <option id="3.5" >B+</option>
        <option id="3.0" >B</option>
        <option id="2.5" >C+</option>
        <option id="2.0" >C</option>
        <option id="1.5" >D</option>
        <option id="0.0" >F</option>
    </select>
        </form>

表格结尾

function showData() {
        var theSelect = GPA.demoSelect;
        var firstGrade = document.getElementById('firstGrade');
        firstGrade.innerHTML = (theSelect[theSelect.selectedIndex].id);
        var grade = theSelect[theSelect.selectedIndex].id;
        var finalGrade = parseFloat(grade);

        var theSelect = GPA2.demoSelect2;
        var secondGrade = document.getElementById('secondGrade');
        secondGrade.innerHTML = (theSelect[theSelect.selectedIndex].id);
        var grade2 = theSelect[theSelect.selectedIndex].id;
        var finalGrade2 = parseFloat(grade2);

        var theSelect = GPA3.demoSelect3;
        var secondGrade = document.getElementById('thirdGrade');
        thirdGrade.innerHTML = (theSelect[theSelect.selectedIndex].id);
        var grade3 = theSelect[theSelect.selectedIndex].id;
        var finalGrade3 = parseFloat(grade3);

        var theSelect = GPA4.demoSelect4;
        var forthGrade = document.getElementById('forthGrade');
        forthGrade.innerHTML = (theSelect[theSelect.selectedIndex].id);
        var grade4 = theSelect[theSelect.selectedIndex].id;
        var finalGrade4 = parseFloat(grade4);


        var FinalCalc = ((finalGrade * 0.2) + (finalGrade2 * 0.3) + (finalGrade3 * 0.2) + (finalGrade4 * 0.3));

        if (FinalCalc >= 3.75) {
            document.getElementById("output2").innerHTML = "A";
        } else if (FinalCalc >= 3.25 && FinalCalc < 3.75) {
            document.getElementById("output2").innerHTML = "B+";

        } else if (FinalCalc >= 2.75 && FinalCalc < 3.25) {
            document.getElementById("output2").innerHTML = "B";

        } else if (FinalCalc >= 2.25 && FinalCalc < 2.75) {
            document.getElementById("output2").innerHTML = "C+";

        } else if (FinalCalc >= 1.75 && FinalCalc < 2.25) {
            document.getElementById("output2").innerHTML = "C";

        } else if (FinalCalc >= 1.25 && FinalCalc < 1.75) {
            document.getElementById("output2").innerHTML = "D";

        } else if (FinalCalc < 1.25) {
            document.getElementById("output2").innerHTML = "F";

        }
        document.getElementById("output").innerHTML = FinalCalc;

    }

1 个答案:

答案 0 :(得分:0)

您提供的部分效果很好:

function showData() {
        var theSelect = GPA.demoSelect;
        var firstGrade = document.getElementById('firstGrade');
        firstGrade.innerHTML = (theSelect[theSelect.selectedIndex].id);
        var grade = theSelect[theSelect.selectedIndex].id;
        var finalGrade = parseFloat(grade);

        var FinalCalc = finalGrade * 0.2
        //var FinalCalc = ((finalGrade * 0.2) + (finalGrade2 * 0.3) + (finalGrade3 * 0.2) + (finalGrade4 * 0.3));


        if (FinalCalc >= 3.75) {
            document.getElementById("output2").innerHTML = "A";
        } else if (FinalCalc >= 3.25 && FinalCalc < 3.75) {
            document.getElementById("output2").innerHTML = "B+";

        } else if (FinalCalc >= 2.75 && FinalCalc < 3.25) {
            document.getElementById("output2").innerHTML = "B";

        } else if (FinalCalc >= 2.25 && FinalCalc < 2.75) {
            document.getElementById("output2").innerHTML = "C+";

        } else if (FinalCalc >= 1.75 && FinalCalc < 2.25) {
            document.getElementById("output2").innerHTML = "C";

        } else if (FinalCalc >= 1.25 && FinalCalc < 1.75) {
            document.getElementById("output2").innerHTML = "D";

        } else if (FinalCalc < 1.25) {
            document.getElementById("output2").innerHTML = "F";

        }
        document.getElementById("output").innerHTML = FinalCalc;

    }

在此处查看:http://jsfiddle.net/wjvkg8q9/15/

请注意,您的函数名为showData(),onchange事件调用showData 2 () 您是否有可能在其余代码中编辑了错误的重复函数?

P.S:我已经在小提琴中评论了其他年级变量,但是正如您所说的,它们是对应的。