Поправка по Advanced JS
Колеги, някой може ли да поясни как е решил следните задания:
- teachingSubject parameter – should set the teacher’s teaching subject. Could be null (if we have a class teacher for example).
- _teachingSubject – a Subject (should be one of the subjects given by Subject.getSubjects()), could be null.
Could be null ме озадачи и съм го решила по особено тъп начин.
- addGradeToStudent(student, gradeParams) – adds a grade to a student.
- student – a Student (only Student instances allowed)
- gradeParams – an object with the following properties:
- subject – A Subject (should be one of the subjects given by Subject.getSubjects()).
If the teacher’s _teachingSubject is null, this property is obligatory because the system wouldn’t know for what subject the grade is.
Otherwise if null, the system will use the _teachingSubject property.
Even if _teachingSubject of the teacher isn’t null, you can still pass this parameter and a grade to the given subject will be added, not the teacher’s teaching subject.
- subject – A Subject (should be one of the subjects given by Subject.getSubjects()).
Ще цитирам моите измишльотини, но нещо куца :) в реализацията ми:
Teacher.prototype.setTeachingSubject = function (teachingSubject) {
if (!teachingSubject) {
this._teachingSubject = null;
} else if((teachingSubject instanceof schoolSystem.Subject) && schoolSystem.Subject.isValidSubject(teachingSubject)) {
this._teachingSubject = teachingSubject;
} else {
throw new ArgumentException('Subject');
}
}
Teacher.prototype.addGradeToStudent = function (student, gradeParams) {
if(!(student instanceof schoolSystem.Student)) {
throw new ArgumentException('Student');
}
if (!gradeParams.subject) {
this.gradeParams.subject = null;
} else if(!((gradeParams.subject instanceof schoolSystem.Subject) && schoolSystem.Subject.isValidSubject(gradeParams.subject))) {
throw new ArgumentException('Subject');
} else if(typeof gradeParams.mark !== 'number') {
throw new ArgumentException('Grade');
} else if(!(gradeParams.semester instanceof schoolSystem.Semester)) {
throw new ArgumentException('Semester');
}
var grade = new schoolSystem.Grade(gradeParams.mark, gradeParams.subject, gradeParams.semester);
if(grade instanceof schoolSystem.Grade) {
student.addGradeToStudent(grade);
}
}
Не знам за вас, но сега си откривам уникално тъпи грешки - първо две незатворени функции с }.
Второ вместо this.setTeachingSubject(teachingSubject); съм написала в конструктора teachingSubject.setTeachingSubject(teachingSubject);
И трето вместо функцията на студента за добавяна на оценки съм пльоснала функцията на учителя за добавяне на оценки:
student.addGradeToStudent(grade); вместо student.addGrades(grade);
И като махнах валидацията, която явно е оплескана - всичко тръгна. :) Дано поне останалите са внимавали повече от мен! Успех!