Как корректно передать результат работы метода в другой метод?
Как корректно передать результат методов countAmountOfClasses и countAmountOfVisits в переменную averageVisit которая находиться в методе summary.
На данный момент averageVisit выдает NaN.
class Student {
constructor(firstName,lastName,yearOfBirth,arrayOfGrades) {
this.firstName = firstName;
this.lastName = lastName;
this.yearOfBirth = yearOfBirth;
this.arrayOfGrades = arrayOfGrades;
}
//______Возраст студента______//
getAge () {
return console.log(`${this.firstName}'s old is ${2023 - this.yearOfBirth} years`);
}
//______Средний бал______//
averageMark () {
let sum = 0;
for (let i = 0; i < this.arrayOfGrades.length; i++) {
sum = sum + this.arrayOfGrades[i];
}
return console.log(`${this.firstName}'s average mark is ${sum / this.arrayOfGrades.length}`);
}
}
//______Экземпляры студентов______//
const student = new Student('Dmitriy','Yaroshchuk',2001,[70,80,90,100,90,90,99,100,95,100]);
const student1 = new Student('Andrew','Kavetsky',2000,[90,90,90,90,90,90,100,100,95,93]);
const student2 = new Student('Diana','Koko',1999,[70,70,70,70,70,70,75,75,75,93]);
//______Вызываю методы______//
student.getAge();
student1.getAge();
student2.getAge();
student.averageMark();
student1.averageMark();
student2.averageMark();
class Visit extends Student {
constructor(firstName, lastName, yearOfBirth, arrayOfGrades) {
super(firstName, lastName, yearOfBirth, arrayOfGrades);
this.visitinMagazine = [];
}
//______Используется когда студент был на занятие______//
present () {
if (this.visitinMagazine.length < 26) {
this.visitinMagazine.push(true);
return this;
} else {
return this.visitinMagazine.pop();
}
}
//______Используется когда студент НЕ был на занятие______//
absent () {
if (this.visitinMagazine.length < 26) {
this.visitinMagazine.push(false);
return this;
} else {
return this.visitinMagazine.pop();
}
}
}
//______Экземпляры посейщения______//
const visit = new Visit('Dmitriy', 'Yaroshchuk', 2001, [70, 80, 90, 100, 90, 90, 99, 100, 95, 100])
const visit1 = new Visit('Andrew', 'Kavetsky', 2000, [90, 90, 90, 90, 90, 90, 100, 100, 95, 93])
const visit2 = new Visit('Diana', 'Koko', 1999, [70, 70, 70, 70, 70, 70, 75, 75, 75, 93])
//______Посейщение уроков______//
visit.present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().absent().absent();
// const callOfVisitnMagazine = [...visit.visitinMagazine];
//______Посейщение уроков______//
visit1.absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().present().present();
// const callOfVisitnMagazine1 = [...visit1.visitinMagazine];
//______Посейщение уроков______//
visit2.present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().absent().absent().absent().absent().absent();
// const callOfVisitnMagazine2 = [...(visit2.visitinMagazine)];
class Calculation extends Visit {
constructor(firstName, lastName, yearsOfBirth, arrayOfGrades) {
super(firstName, lastName, yearsOfBirth, arrayOfGrades);
}
//______Колличество занятий______//
countAmountOfClasses () {
return console.log(this.arrayOfGrades.length);
}
//______Колличество посищений______//
countAmountOfVisits (visitinMagazine) {
return console.log(visitinMagazine.filter((element) => element === true).length);
}
//______Проверяем среднюю оценку и посейщение______//
summary () {
const averageVisit = this.countAmountOfVisits / this.countAmountOfClasses;
if (this.averageMark > 90 && averageVisit > 0.9) {
return console.log('Cool!');
} else if (this.averageMark > 90 || averageVisit > 0.9) {
return console.log('Good, but it can be better!');
} else {
return console.log('Radish');
}
}
}
//______Экземпляры расчетов______//
const calculate = new Calculation('Dmitriy', 'Yaroshchuk', 2001, [70, 80, 90, 100, 90, 90, 99, 100, 95, 100]);
const calculate1 = new Calculation('Andrew', 'Kavetsky', 2000, [90, 90, 90, 90, 90, 90, 100, 100, 95, 93]);
const calculate2 = new Calculation('Diana', 'Koko', 1999, [70, 70, 70, 70, 70, 70, 75, 75, 75, 93]);
calculate.countAmountOfClasses();
calculate1.countAmountOfClasses();
calculate2.countAmountOfClasses();
calculate.countAmountOfVisits(visit.visitinMagazine);
calculate1.countAmountOfVisits(visit1.visitinMagazine);
calculate2.countAmountOfVisits(visit2.visitinMagazine);
calculate.summary();
calculate1.summary();
calculate2.summary();
Ответы (1 шт):
Автор решения: SwaD
→ Ссылка
Вы немного заблудились в классах... Каждый экземпляр класса сущестувет обособленно.
Вам надо создавать экземпляр из класса Calculation и уже дальше вызывать все необходимые методы
class Student {
constructor(firstName,lastName,yearOfBirth,arrayOfGrades) {
this.firstName = firstName;
this.lastName = lastName;
this.yearOfBirth = yearOfBirth;
this.arrayOfGrades = arrayOfGrades;
this.age = new Date().getFullYear() - this.yearOfBirth;
this.avgMark = arrayOfGrades.reduce((acc, it) => acc += it, 0)/arrayOfGrades.length;
}
//______Возраст студента______//
getAge () {
return `${this.firstName}'s old is ${this.age} years`
}
//______Средний бал______//
averageMark () {
return `${this.firstName}'s average mark is ${this.avgMark}`;
}
}
class Visit extends Student {
constructor(firstName, lastName, yearOfBirth, arrayOfGrades) {
super(firstName, lastName, yearOfBirth, arrayOfGrades);
this.visitinMagazine = [];
}
//______Используется когда студент был на занятие______//
present () {
this.visitinMagazine.length < 26 ? this.visitinMagazine.push(true) : this.visitinMagazine.pop();
return this;
}
//______Используется когда студент НЕ был на занятие______//
absent () {
this.visitinMagazine.length < 26 ? this.visitinMagazine.push(false) : this.visitinMagazine.pop();
return this
}
}
class Calculation extends Visit {
constructor(firstName, lastName, yearsOfBirth, arrayOfGrades) {
super(firstName, lastName, yearsOfBirth, arrayOfGrades);
}
//______Проверяем среднюю оценку и посейщение______//
summary () {
const averageVisit = this.arrayOfGrades.length / this.visitinMagazine.filter(el => !!el).length
if (this.avgMark > 90 && averageVisit > 0.9) {
return `${this.firstName} Cool!`;
} else if (this.avgMark > 90 || averageVisit > 0.9) {
return `${this.firstName} Good, but it can be better!`;
} else {
return `${this.firstName} Radish`;
}
}
}
//______Экземпляры расчетов______//
const calculate = new Calculation('Dmitriy', 'Yaroshchuk', 2001, [70, 80, 90, 100, 90, 90, 99, 100, 95, 100]);
const calculate1 = new Calculation('Andrew', 'Kavetsky', 2000, [90, 90, 90, 90, 90, 90, 100, 100, 95, 93]);
const calculate2 = new Calculation('Diana', 'Koko', 1999, [70, 70, 70, 70, 70, 70, 75, 75, 75, 93]);
// Общая информация
console.log(calculate.getAge());
console.log(calculate1.getAge());
console.log(calculate2.getAge());
console.log(calculate.averageMark());
console.log(calculate1.averageMark());
console.log(calculate2.averageMark());
//______Посейщение уроков______//
calculate.present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().absent().absent();
calculate1.absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().absent().present().present();
calculate2.present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().present().absent().absent().absent().absent().absent();
console.log(calculate.summary());
console.log(calculate1.summary());
console.log(calculate2.summary());