成绩统计计算器

时间:2015-06-23 12:43:09

标签: javascript html css calculator

我想在我的javascript代码上获得一些帮助。我制作了一个成绩统计计算器,显示了以下结果: 最小 - 最高学生成绩 最小 - 最大学生平均分 最小 - 最高课程成绩 最小 - 最大课程平均成绩

您可以在此处访问 - > http://jsbin.com/qirefe/edit?html,css,js,output并按“显示结果”按钮查看我的输出。 (您可以更改名称和等级以获得不同的输出)

我的问题是我无法弄清楚为什么它没有在Min-Max课程等级上显示正确的课程名称,尽管它显示了正确的成绩。另外我无法弄清楚为什么它错误地计算了最小和最大课程的平均成绩并显示相应的课程名称错误..

任何帮助都将非常感谢:)

.js代码:

var Course0 = Array(6);
var Course1 = Array(6);
var Course2 = Array(6);
var Student = Array(6);

var CMap = [Course0, Course1, Course2];
var NMap = ["Course0", "Course1", "Course2"];

var showResults = function () {

    var Rows = document.getElementsByClassName("srow");

    for (var i = 1; i < Rows.length - 1; i++) {

        var values = Rows[i].getElementsByTagName("input");
        Student[i - 1] = values[0].value;

        for (var j = 1; j < values.length; j++) {
            CMap[j - 1][i - 1] = values[j].value;
        }
    }

    var MinID = MaxID = AvgMinID = AvgMaxID = 0;
    var Min = Max = AvgMin = AvgMax = undefined;

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

        var c0 = Course0[i];
        var c1 = Course1[i];
        var c2 = Course2[i];

        var lessonMin = Math.min(c0, c1, c2);
        var lessonMax = Math.max(c0, c1, c2);

        if ((lessonMin <= Min) || (typeof Min === "undefined")) {
            MinID = i;
            Min = lessonMin;
        }

        if ((lessonMax >= Max) || (typeof Max === "undefined")) {
            MaxID = i;
            Max = lessonMax;
        }


        var Avg = Math.avg(c0, c1, c2);

        if ((Avg < AvgMin) || (typeof AvgMin === "undefined")) {
            AvgMinID = i;
            AvgMin = Avg;
        }

        if ((Avg > AvgMax) || (typeof AvgMax === "undefined")) {
            AvgMaxID = i;
            AvgMax = Avg;
        }

    }

    var Wrapper = document.getElementById("student-results");
    Wrapper.innerHTML = "";


    Wrapper.innerHTML += "<span>The Student with lower grade is: " + Student[MinID] + ", Equals To " + Min + "</span>";
    Wrapper.innerHTML += "<span>The Student with higher grade is: " + Student[MaxID] + ", Equals To " + Max + "</span>";

    Wrapper.innerHTML += "<hr />";


    Wrapper.innerHTML += "<span>The Student with lower average grade is: " + Student[AvgMinID] + ", Equals To " + AvgMin + "</span>";
    Wrapper.innerHTML += "<span>The Student with higher average grade is: " + Student[AvgMaxID] + ", Equals To " + AvgMax + "</span>";

    var CourseMin = CourseMinID = CourseMax = CourseMaxID = CourseAvgMin = CourseAvgMinID = CourseAvgMax = CourseAvgMaxID = 0;

    CourseMin = CourseMax = CourseAvgMin = CourseAvgMax = undefined;

    for (var i = 0, j = 0; i < Student.length; i++, j += .5) {

        var c0 = Course0;
        var c1 = Course1;
        var c2 = Course2;

        var CheckMin = Math.min(c0[i], c1[i], c2[i]);

        if (CourseMin > CheckMin || (typeof CourseMin === "undefined")) {
            CourseMin = CheckMin;
            CourseMinID = i;
        }

        var CheckMax = Math.max(c0[i], c1[i], c2[i]);

        if (CourseMax < CheckMax || (typeof CourseMax === "undefined")) {
            CourseMax = CheckMax;
            CourseMaxID = parseInt(j);
        }

        var Avg = Math.avg(c0[i], c1[i], c2[i]);

        if (Avg < CourseAvgMin || (typeof CourseAvgMin === "undefined")) {
            CourseAvgMin = Avg;
            CourseAvgMinID = j;
        }

        if (Avg > CourseAvgMax || (typeof CourseAvgMax === "undefined")) {
            CourseAvgMax = Avg;
            CourseAvgMaxID = parseInt(j);
        }

    }

    console.log(CourseMaxID);

    Wrapper.innerHTML += "<hr />";

    Wrapper.innerHTML += "<span>The Course with lower grade have: " + NMap[CourseMinID] + ", Equals To " + CourseMin + "</span>";
    Wrapper.innerHTML += "<span>The Course with higher grade have: " + NMap[CourseMaxID] + ", Equals To " + CourseMax + "</span>";

    Wrapper.innerHTML += "<hr />";


    Wrapper.innerHTML += "<span>The Course with lower average grade have: " + NMap[CourseAvgMinID] + ", Equals To " + CourseAvgMin + "</span>";
    Wrapper.innerHTML += "<span>The Course with higher average grade have: " + NMap[CourseAvgMaxID] + ", Equals To " + CourseAvgMax + "</span>";


    return null;

};

Math.avg = function () {

    var Avg = 0;
    var table = arguments;
    for (var i = 0; i < table.length; i++) {
        Avg += parseFloat(table[i]);
    }
    return parseFloat(Avg / table.length);

};

1 个答案:

答案 0 :(得分:1)

在控制台中检查CourseMaxID和CourseMinID的输出后,CourseMinID的索引为3,但NMap只有3个值(索引为0,1,2)。所以我相信这就是为什么,例如,你看到的原因: "The Course with lower grade have: " + NMap[CourseMinID] + ", Equals To " + CourseMin;未定义 - 因为索引超出范围。

以下是使用CourseMinID和CourseMaxID解决问题的方法: 将CourseMinID的定义更改为Math.floor(j)-1; 并将CourseMaxID更改为等于Math.ceil(j);

您对浮动值调用parseInt()似乎没有预期的后果。

我不完全确定你为什么每次都选择将j增加0.5,但从观察中我注意到,对于CourseMax / CourseMinID,你想要使用我上面提到的计算。

另一个注意事项,对于课程平均值,您实际上是在输出学生的平均值。所以你会想要在那里改变你的逻辑。在我看来,你将水平排的等级作为平均函数的参数:

var Avg = Math.avg(c0[i], c1[i], c2[i]);

这不是您想要解析平均课程平均等级的平均值。我将定义另一个Avg函数(让我们在这里称之为newAvg()),将单个数组(不是多个参数)作为输入,然后在{上调用Math.Min / Math.Max {1}},newAvg(c0)newAvg(c1)

这里有一个updated jsbin link,其中包含课程平均值的工作功能。更改概述:newAvg()已定义为接受并操作一个参数。跟踪CourseAvgMax和CourseAvgMin的索引。请注意,我已经删除了您之前在此jsbin链接中执行的一些其他操作,以便我更容易隔离我正在处理的内容。希望你觉得它很有用!