2014-03-31 119 views
0

我想知道如何將此數組插入表中。這是我的代碼;我想它顯示是這樣的:JavaScript如何將數組插入表中

學生姓名學生馬克學生成績

如果有誰知道怎麼樣,這將不勝感激。

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>"); 

回答

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); 
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> 
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>");