I want to create a class called SchoolCatalog that will hold a collection of schools, e.g. Create an instance of SchoolCatalog for primary, middle, and high schools. (This is one of the problems from Code Academy Introduction To JavaScript). But, they have no proper guide on how to make a Catalog to hold all classes in the forum. So, if anybody can help me out I will be grateful.
JavaScript
x
92
92
1
class School {
2
constructor(name, level, numberOfStudents) {
3
this._name = name
4
this._level = level
5
this._numberOfStudents = numberOfStudents
6
}
7
8
get name() {
9
return this._name
10
}
11
12
get level() {
13
return this._level
14
}
15
16
get numberOfStudents() {
17
return this._numberOfStudents
18
}
19
20
21
set numberOfStudents(studentsNumber) {
22
if (typeof studentNumber === 'number')
23
this._numberOfStudents = studentsNumber
24
else
25
console.log(`Invalid input: numberOfStudents must be set to a Number.`)
26
}
27
28
quickFacts() {
29
console.log(`${this._name} educates ${this._numberOfStudents} students at the ${this._level} school level.`)
30
}
31
32
static pickSubstituteTeacher(substituteTeachers) {
33
const randomTeacher = Math.floor(Math.random() * substituteTeachers.length)
34
console.log(substituteTeachers[randomTeacher])
35
}
36
37
}
38
39
// Primary School
40
41
class PrimarySchool extends School {
42
constructor(name, numberOfStudents, pickupPolicy) {
43
super(name, 'primary', numberOfStudents)
44
this._pickupPolicy = pickupPolicy
45
}
46
47
get pickupPolicy() {
48
return this._pickupPolicy
49
}
50
}
51
52
53
class HighSchool extends School {
54
constructor(name, numberOfStudents, sportsTeams) {
55
super(name,'high', numberOfStudents)
56
this._sportsTeams = sportsTeams
57
}
58
59
get sportsTeams() {
60
this._sportsTeams.forEach(team => {
61
console.log(team)
62
})
63
}
64
}
65
66
const lorraineHansbury = new PrimarySchool('Lorraine Hansbury', 514, 'Students must be picked up by a parent, guardian, or a family member over the age of 13.')
67
68
lorraineHansbury.quickFacts()
69
PrimarySchool.pickSubstituteTeacher(['Jamal Crawford', 'Lou Williams', 'J. R. Smith', 'James Harden', 'Jason Terry', 'Manu Ginobli'])
70
71
72
const alSmith = new HighSchool('Al E. Smith', 415, ['Baseball', 'Basketball', 'Volleyball', 'Track and Field'])
73
alSmith.sportsTeams
74
75
76
// My Attempt To Make Collection of Classes
77
78
79
/*class SchoolCatalog {
80
constructor(schools) {
81
this._schools = schools
82
}
83
84
get schools() {
85
this._schools.forEach(school => {
86
console.log(school)
87
})
88
}
89
}
90
91
92
const catalog = new SchoolCatalog([''])*/
Advertisement
Answer
Is there an issue with your implementation ?
JavaScript
1
13
13
1
class Catalog{
2
constructor(){
3
this.schools = []
4
}
5
addSchool(school){
6
if(!this[school._name]){
7
// reference for easy access
8
this[school._name]=school;
9
}
10
this.schools.push(school);
11
}
12
}
13
If I needed a list I could iterate over and still easily get single schools from this is how i would do it ,
That said depending on your requirements this could be totally wrong.
Your implementation at first blush doesn’t look to be incorrect , is it giving you a error?