JavaScript如何将数组插入表中

时间:2014-03-31 05:43:25

标签: javascript arrays

我想知道如何将此数组插入表中。这是我的代码;我希望它显示如下:

  

学生姓名学生成绩学生成绩

如果有人知道如何,我们将不胜感激。

var gradeAwarded;
var max;
var A =0;
var B =0;
var C=0;
var F=0;
var Sum = 0
var average
var repeat, studentArr = [], markArr = [];

while (repeat !== 'n'){
    studentArr.push(prompt("Enter Student Name: ", "Ross"));
    markArr.push(parseInt (prompt("Enter Student mark: ", 50)));

    if (markArr <0 || markArr >100){
        alert("Grate out of bounds");
    }else if (markArr >= 83){
        gradeAwarded = "A";
        A = A + 1;
    }else if (markArr >= 70){
        gradeAwarded = "B";
        B = B+1;
    }else if (markArr >= 50){
        gradeAwarded = "C";
        C = C+1;    
    }else if (markArr >= 0){
        gradeAwarded = "F";
        F = F+1;                                                        
    }
     repeat = prompt ("Do you want to enter another student: y/n");

}  

Array.prototype.max = function() {
  return Math.max.apply(Math, markArr);
};

Array.prototype.min = function() {
  return Math.min.apply(Math, markArr);
};
for(var x = 0; x < markArr.length; x ++)
{
  Sum = Sum + markArr[x]; 
}

average = Sum / markArr.length; 
var min = markArr.min();
var max = markArr.max();

for(var i = 0; i < markArr.length; i++)
document.write(<tr> markArr[i] </tr>);
    document.write("The Highest mark was : "+max+"<br>");
    document.write("The Lowest mark was : "+min +"<br>");
    document.write("The Average mark was : "+average+"<br>");
    document.write("number A grades : "+A+"<br>");
    document.write("number B grades : "+B+"<br>");
    document.write("number C grades : "+C+"<br>");
    document.write("number F grades : "+F+"<br>");

4 个答案:

答案 0 :(得分:1)

希望这有帮助

            var markAwarded;
            var gradeAwarded;
            var max;
            var A = 0;
            var B = 0;
            var C = 0;
            var F = 0;
            var Sum = 0
            var average
            var repeat, studentArr = [], markArr = [], gradeArr = [];
            while (repeat !== 'n') {
                studentArr.push(prompt("Enter Student Name: ", "Ross"));
                markAwarded = parseInt(prompt("Enter Student mark: ", 50));
                markArr.push(markAwarded);

                if (markAwarded < 0 || markAwarded > 100) {
                    alert("Grate out of bounds");
                } else if (markAwarded >= 83) {
                    gradeAwarded = "A";
                    A = A + 1;
                } else if (markAwarded >= 70) {
                    gradeAwarded = "B";
                    B = B + 1;
                } else if (markAwarded >= 50) {
                    gradeAwarded = "C";
                    C = C + 1;
                } else if (markAwarded >= 0) {
                    gradeAwarded = "F";
                    F = F + 1;
                }
                gradeArr.push(gradeAwarded);
                repeat = prompt("Do you want to enter another student: y/n");

            }

            Array.prototype.max = function () {
                return Math.max.apply(Math, markArr);
            };

            Array.prototype.min = function () {
                return Math.min.apply(Math, markArr);
            };
            for (var x = 0; x < markArr.length; x++) {
                Sum = Sum + markArr[x];
            }

            average = Sum / markArr.length;
            var min = markArr.min();
            var max = markArr.max();

            document.write("<table style='border:1px solid black;'><tr><th>Student Name</th><th>Student Mark</th><th>Student Grade </th></tr>");
            for (var i = 0; i < markArr.length; i++) {
                document.write("<tr><td>" + studentArr[i] + "</td><td>" + markArr[i] + "</td><td>" + gradeArr[i] + "</td> </tr>");
            }
            document.write("</table>");
            document.write("The Highest mark was : " + max + "<br>");
            document.write("The Lowest mark was : " + min + "<br>");
            document.write("The Average mark was : " + average + "<br>");
            document.write("number A grades : " + A + "<br>");
            document.write("number B grades : " + B + "<br>");
            document.write("number C grades : " + C + "<br>");
            document.write("number F grades : " + F + "<br>");

答案 1 :(得分:0)

如果你想纯粹从javascript创建表,那么就有这样的函数:

var body = document.getElementsByTagName('body')[0];
var table = document.createElement('table');
var tr = document.createElement('tr');
var td = document.createElement('td');
td.nodeValue = someDataToDisplayInTheCell;

tr.appendChild(td);
table.appendChild(tr);
body.appendChild(body);

答案 2 :(得分:0)

试着看一下:Print out Javascript array in table

有很多方法可以从数组创建表。

答案 3 :(得分:0)

试试此代码

  <script>
  var gradeAwarded;
  var max;
  var A =0;
  var B =0;
  var C=0;
  var F=0;
  var Sum = 0
  var average
  var repeat, studentArr = [], markArr = [];

  while (repeat !== 'n'){
      studentArr.push(prompt("Enter Student Name: ", "Ross"));
      markArr.push(parseInt (prompt("Enter Student mark: ", 50)));

      if (markArr <0 || markArr >100){
          alert("Grate out of bounds");
      }else if (markArr >= 83){
          gradeAwarded = "A";
          A = A + 1;
      }else if (markArr >= 70){
          gradeAwarded = "B";
          B = B+1;
      }else if (markArr >= 50){
          gradeAwarded = "C";
          C = C+1;    
      }else if (markArr >= 0){
          gradeAwarded = "F";
          F = F+1;                                                        
      }
       repeat = prompt ("Do you want to enter another student: y/n");

  }  

  Array.prototype.max = function() {
    return Math.max.apply(Math, markArr);
  };

  Array.prototype.min = function() {
    return Math.min.apply(Math, markArr);
  };
  for(var x = 0; x < markArr.length; x ++)
  {
    Sum = Sum + markArr[x]; 
  }

  average = Sum / markArr.length; 
  var min = markArr.min();
  var max = markArr.max();
  document.write("<table>");
  for(var i = 0; i < markArr.length; i++){
    document.write("<tr>");
    document.write("<td>"+  studentArr[i] + '</td>');
    document.write("<td>"+  markArr[i] + '</td>');
    if (markArr[i] >= 83){
          gradeAwarded = "A";
      }else if (markArr[i] >= 70){
          gradeAwarded = "B";
      }else if (markArr[i] >= 50){
          gradeAwarded = "C";
      }else if (markArr[i] >= 0){
          gradeAwarded = "F";
      }
      document.write("<td>"+  gradeAwarded + '</td>');
    }
      document.write("</table>");
      document.write("The Highest mark was : "+max+"<br>");
      document.write("The Lowest mark was : "+min +"<br>");
      document.write("The Average mark was : "+average+"<br>");
      document.write("number A grades : "+A+"<br>");
      document.write("number B grades : "+B+"<br>");
      document.write("number C grades : "+C+"<br>");
      document.write("number F grades : "+F+"<br>");

  </script>