I don't know what's wrong with it. Please help me I am trying to make a table every time the user input the name and select a course. if the grade is more than 50 show the grade and print "pass" and if it's not show the grade and "fail" and the grade is always a random number and I want to save it with local storage
`let studentTable = document.getElementById("table-id"); let form = document.getElementById("form-id");
function getRandomNumber() {
return Math.floor(Math.random() * (100 - 0 + 1) + 0);
}
function Student(name, course) { // pass the user input in the parameter
this.name = name;
this.course = course;
this.grade = getRandomNumber(); //call the random number (shortcut way)
Student.prototype.allStudents.push(this);
}
Student.prototype.allStudents = [];
Student.prototype.calculatGrades = function grade() {
this.grade = getRandomNumber();
}
Student.prototype.rendeTable = function () {
let tableRow = document.createElement("tr");
studentTable.appendChild(tableRow);
console.log(tableRow);
let firstColum = document.createElement("td");
tableRow.appendChild(firstColum);
firstColum.textContent = this.name;
console.log(firstColum);
let secondColum = document.createElement("td");
tableRow.appendChild(secondColum);
secondColum.textContent = this.course;
let thiredColum = document.createElement("td");
tableRow.appendChild(thiredColum);
thiredColum.textContent = this.grade;
let forthColum = document.createElement("td");
tableRow.appendChild(forthColum);
if (this.grade >= 50) {
forthColum.textContent = "Pass";
} else {
forthColum.textContent = "faill";
}
}
let tableHeader = document.createElement("tr");
studentTable.appendChild(tableHeader);
let firstHead = document.createElement("th");
tableHeader.appendChild(firstHead);
firstHead.textContent = "Student Name";
let secondHead = document.createElement("th");
tableHeader.appendChild(secondHead);
secondHead.textContent = "Course"
let thiredHead = document.createElement("th");
tableHeader.appendChild(thiredHead);
thiredHead.textContent = "Grade";
let forthHead = document.createElement("th");
tableHeader.appendChild(forthHead);
forthHead.textContent = "Course P/F";
// add event lisener to the submit button
form.addEventListener("submit", submitter);
function submitter(event) {
event.preventDefalut();
let newStudentName = event.target.name.value;
let newStudentCourse = event.target.course.value;
console.log(newStudentCourse);
let newStudent = new Student(newStudentName, newStudentCourse);
newStudent.calculatGrades();
newStudent.rendeTable();
// newStudent.randomNumber();
localStorage.setItem("student", JSON.stringify(Student.prototype.allStudents));
console.log(student);
}
let getData = JSON.parse(localStorage.getItem("student"))
console.log(getData);
if (getData !== null) {
for (let i = 0; i <= getData.length; i++) {
let perviousStudent = new Student(getData[i].name, getData[i].course);
perviousStudent.calculatGrades();
perviousStudent.rendeTable();
}
} `
Aucun commentaire:
Enregistrer un commentaire