Can anyone teach me how to sort the data that the user input? I am planning to sort the data (marks) from the table and get results e.g. sort the marks from highest to lowest. My current codes that needs help to sort.
//Brandon Tan P1514627
//Zong Wei P151
//Javascript
//Table headers
document.write("<table><th style='width:25px'>No.</th>");
document.write("<th style='width:100px'>Name</th>");
document.write("<th style='width:100px'>Attendance</th>");
document.write("<th style='width:100px'>Homework</th>");
document.write("<th style='width:100px'>Midterm Test</th>");
document.write("<th style='width:100px'>Final Exam</th>");
document.write("<th style='width:100px'>Final Grade</th>");
document.write("<th style='width:100px'>Letter Grade</th>");
//Setting the component weightage
do{
var weightAtt = parseFloat(prompt("Enter the weightage of attendance"));
while (weightAtt<=0 || weightAtt>=1 || isNaN(weightAtt)) {
alert("The weightage of attendance must a positive number less than 1");
var weightAtt = parseFloat(prompt("Enter the weightage of attendance"));
}
var weightHw = parseFloat(prompt("Enter the weightage of homework"));
while (weightHw<=0 || weightHw>=1 || isNaN(weightHw)) {
alert("The weightage of homework must a positive number less than 1");
var weighthw = parseFloat(prompt("Enter the weightage of homework"));
}
var weightMdt = parseFloat(prompt("Enter the weightage of midterm test"));
while (weightMdt<=0 || weightMdt>=1 || isNaN(weightMdt)) {
alert("The weightage of midterm test must a positive number less than 1");
var weightMdt = parseFloat(prompt("Enter the weightage of midterm test"));
}
var weightFx = parseFloat(prompt("Enter the weightage of final exam"));
while (weightFx<=0 || weightFx>=1 || isNaN(weightFx)) {
alert("The weightage of final exam must a positive number less than 1");
var weightFx = parseFloat(prompt("Enter the weightage of final exam"));
}
if ([weightAtt + weightHw + weightMdt + weightFx] != 1) {
alert("The total weightage of all components must be equals to 1!");
}
}
while ([weightAtt + weightHw + weightMdt + weightFx] != 1);
//----------------------------------------------------------------------------
//Enter the number of students you want to enter the grades for
var numStudents = parseInt(prompt("Enter the number of students"));
while (numStudents<=0 || isNaN(numStudents)) {
alert("The number of students must a postive integer!");
var numStudents = parseInt(prompt("Enter the number of students"));
}
//Loop
for(var i = 0; i < numStudents; i++){
var no = i + 1;
document.write("<tr><td style='width:25px'>" + no + "</td>");
var name = prompt("Enter the student's name");
document.write("<td style='width:100px'>" + name + "</td>");
//----------------------------------------------------------------------------
var att = parseFloat(prompt("Enter " + name + "'s attendance"));
while (att<0 || att>100 || isNaN(att)) {
alert("Attendance marks must be a number between 0-100!");
var att = parseFloat(prompt("Enter " + name + "'s attendance"));
}
document.write("<td style='width:100px'>" + att + "</td>");
//---------------------------------------------------------------------------
var hw = parseFloat(prompt("Enter " + name + "'s homework"));
while (hw<0 || hw>100 || isNaN(hw)) {
alert("Homework marks must be a number between 0-100!");
var hw = parseFloat(prompt("Enter " + name + "'s homework"));
}
document.write("<td style='width:100px'>" + hw + "</td>");
//----------------------------------------------------------------------------
var mdt = parseFloat(prompt("Enter " + name + "'s midterm test"));
while (mdt<0 || mdt>100 || isNaN(mdt)) {
alert("Midterm Test marks must be a number between 0-100!");
var mdt = parseFloat(prompt("Enter " + name + "'s midterm test"));
}
document.write("<td style='width:100px'>" + mdt + "</td>");
//----------------------------------------------------------------------------
var fx = parseFloat(prompt("Enter " + name + "'s final exam"));
while (fx<0 || fx>100 || isNaN(fx)) {
alert("Final Exam marks must be a number between 0-100!");
var fx = parseFloat(prompt("Enter " + name + "'s final exam"));
}
document.write("<td style='width:100px'>" + fx + "</td>");
//---------------------------------------------------------------------------
var fg = att * weightAtt + hw * weightHw + mdt * weightMdt + fx * weightFx
document.write("<td style='width:100px'>" + fg + "</td>");
//----------------------------------------------------------------------------
var lg = 0;
if(fg >= 80){
lg = "A";
}
else if(fg < 80 && fg >= 70){
lg = "B";
}
else if(fg < 70 && fg >= 60){
lg = "C";
}
else if(fg < 60 && fg >=50){
lg = "D";
}
else if (fg >= 0 && fg < 50){
lg = "F";
}
document.write("<td style='width:100px'>" + lg + "</td></tr>");
}
document.write("</table>");