This repository has been archived by the owner on Dec 20, 2022. It is now read-only.
forked from netology-code/bjs-homeworks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.js
140 lines (124 loc) · 2.9 KB
/
task.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
'use strict'
//Задача 2.3.1
class PrintEditionItem {
constructor(name, releaseDate, pagesCount) {
this.name = name;
this.releaseDate = releaseDate;
this.pagesCount = pagesCount;
this.state = 100;
this.type = null;
}
fix(){
this.state *= 1.5
};
set state(number) {
if (number < 0) {
this._state = 0
} else if (number > 100) {
this._state = 100
} else {
this._state = number;
}
}
get state() {
return this._state;
}
}
class Magazine extends PrintEditionItem {
constructor(name, releaseDate, pagesCount) {
super(name, releaseDate, pagesCount);
this.type = "magazine";
}
}
class Book extends PrintEditionItem {
constructor(author,name, releaseDate, pagesCount) {
super(name, releaseDate, pagesCount);
this.type = "book";
this.author = author;
}
}
class NovelBook extends Book{
constructor(name, releaseDate, pagesCount) {
super(name, releaseDate, pagesCount);
this.type = "novel";
}
}
class FantasticBook extends Book{
constructor(name, releaseDate, pagesCount) {
super(name, releaseDate, pagesCount);
this.type = "fantastic";
}
}
class DetectiveBook extends Book{
constructor(name, releaseDate, pagesCount) {
super(name, releaseDate, pagesCount);
this.type = "detective";
}
}
//Задача 2.3.2
class Library {
constructor(name, books) {
this.name = name;
this.books = [];
};
addBook(book) {
if (book.state > 30) {
this.books.push(book);
}
};
findBookBy(type, value){
for (let i = 0; i < this.books.length; i++){
if (this.books[i][type] === value)
return this.books[i];
}
return null
};
giveBookByName(bookName){
for (let i = 0; i < this.books.length; i++){
const item = this.books.findIndex(book => book.name === bookName);
if (item != -1)
return this.books.splice(item, 1)[0];
}
return null
};
}
//Задача 2.3.3
class StudentLog {
constructor(name) {
this.name = name;
this.grades = {};
}
getName() {
return this.name;
}
addGrade(grade, subject) {
if (grade < 1 || grade > 5 || typeof grade !== 'number') {
return `Вы пытались поставить оценку "${grade}" по предмету "${subject}". Допускаются только числа от 1 до 5.`
} else if (this.grades[subject]) {
this.grades[subject].push(grade);
return this.grades[subject].length;
}else {
this.grades[subject] = [grade];
return 1;
}
}
getAverageBySubject(subject) {
if (Object.keys(this.grades).includes(subject)) { //если обьект grades включает в себя subject
let sum = 0;
for (let rating of this.grades[subject]) {
sum += rating;
}
return sum / this.grades[subject].length;
} else {
return 0;
}
}
getTotalAverage() {
let totalAverage = 0;
for (let element in this.grades) {
totalAverage += this.getAverageBySubject(element);
}
totalAverage = totalAverage / Object.keys(this.grades).length;
return totalAverage;
}
}