I coding demo student management . But have problem below, please help me resolve them.
The requirement
id
Name
Age
Point
This is my code:
//
function Student(id, name, age, point) {
this.id = id;
this.name = name;
this.age = age;
this.point = point;
}
Student.prototype = {
setId: function (value) {
this.id = value;
},
getId: function () {
return this.id;
},
setName: function (value) {
this.name = value;
},
getName: function () {
return this.name;
},
setAge: function (value) {
this.age = value;
},
getAge: function () {
return this.age;
},
setPoint: function (value) {
this.point = value;
},
getPoint: function () {
return this.point;
},
};
function StudentController(student) {
this.listStudent = [];
this.id = 1;
}
StudentController.prototype = {
addNew: function (name, age, point) {
var student = new Student(this.id, name, age, point);
this.listStudent.push(student);
this.id += 1;
return student;
},
This is function find id . But it always return last id in array object.
findId: function (id) {
var student = null;
for (var i = 0; i < this.listStudent.length; i++) {
if (id = this.listStudent[i].getId()) {
student = this.listStudent[i];
}
}
return student;
},
This is function edit student. But it can't getId
from function findId();
editStudent: function (student) {
var oldStudent = this.findId(student.getId());
console.log('oldStudent', oldStudent);
for (var x = 0; x < this.listStudent.length; x++) {
if (oldStudent = this.listStudent[x].getId()) {
this.listStudent[x] = student;
}
}
},
This function same too editStudent()
function.
deleteStudent: function (student) {
var crrentStudent = this.findId(student.getId());
for (var y = 0; y < this.listStudent.length; y++) {
if (crrentStudent.getId() === this.listStudent[y]) {
this.listStudent.splice(y, 1);
}
}
},
This this function sort point of student > = 5. But look like not working :(
// find point student > = 5
findByPoint: function (point) {
var point = '';
for (var i = 0; i < this.listStudent.length; i++) {
if (this.listStudent[i].getPoint() >= point) {
return point;
}
}
},
showStudent: function () {
console.table(this.listStudent);
},
};
var studentController = new StudentController();
studentController.addNew("Hanh", 20, 8);
studentController.findId(1);
studentController.editStudent();
studentController.deleteStudent();
Please help me resolve and explanation. Thank you so much !!
The problem is pretty straight forward, for a comparison operator you need ==
in if condition
, =
is for assignment and adding
Change findId()
function
findId: function (id) {
var student = null;
for (var i = 0; i < this.listStudent.length; i++) {
if (id == this.listStudent[i].getId()) { // change here
student = this.listStudent[i];
}
}
return student;
},
and in editStudent
function
editStudent: function (student) {
var oldStudent = this.findId(student.getId());
console.log('oldStudent', oldStudent);
for (var x = 0; x < this.listStudent.length; x++) {
if (oldStudent == this.listStudent[x].getId()) { //change here
this.listStudent[x] = student;
}
}
},
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.