将数据写入包含未知行数的表

时间:2016-11-23 18:11:33

标签: javascript

我是一个非常基本的"介绍JS"当然,我对这个问题的解决需要同样基本和过时。

练习应该显示一个带有学生姓名和标题的标题的表格。得分为90或以上的得分。 (数据来自另一个js文件。)

然后应该显示学生总数和得分为90分或以上的学生总数。我的数学和如果陈述似乎有效,但显示不是......远远不够。任何帮助将不胜感激。

const ZERO = 0;
const NINETY_PERCENT = 90;

var studentName;
var studentScore;
var totalStudents;
var totalStudentsWithAs;
var studentRecords;

studentRecords = openStudentExamRecords();

function initializeExamRecords() {
    totalStudents = ZERO;
    totalStudentsWithAs = ZERO;
}

function startTable() {
    document.write("<th>Student Name</th>");
    document.write("<th>Exam Score</th>");
}

function printRecords() {
    initializeExamRecords();
    startTable();
    while (studentRecords.readNextRecord()) {
        studentName = studentRecords.getStudentName();
        studentScore = studentRecords.getStudentScore();
        totalStudents++;

        if (studentScore >= NINETY_PERCENT) {
            document.write("</td><td>");
            document.write(studentName);
            document.write("</td><td>");
            document.write(studentScore);
            totalStudentsWithAs++;
            totalStudents = Number(totalStudents);
            totalStudentsWithAs = Number(totalStudentsWithAs); alert("");
        }
    }
}

function endTable() {
    document.write("</td></tr>");
}

function printTotals() {
    printRecords();
    document.write("</td><td>");
    document.write("<tr><td colspan='5'>&nbsp;</td></tr>");
    document.write("<tr><td colspan='5'>Total Students: <strong>" +
        totalStudents + "</strong></td></tr>");
    document.write("<tr><td colspan='5'>Total Sudents with A's: <strong>" +
        totalStudentsWithAs + "</strong></td></tr>");
    document.write("</td><tr>");
}

function endTable() {
    document.write("</td></tr>");
}

printTotals();
endTable();

0 个答案:

没有答案