School catalogue


#1

class School {
constructor(name, level, numberOfStudents) {
this._name = name;
this._level = level;
this._numberOfStudents = numberOfStudents;
}

get name() {
return this._name;
}

get level() {
return this._level;
}

get numberOfStudents() {
return this._numberOfStudents;
}

set numberOfStudent(num) {
if (typeof num === number) {
this._numberOfStudents = num;
} else {
console.log(‘Invalid input: numberOfStudents must be set to a Number.’);
}
}

quickFacts() {
console.log(${this.name} educates ${this.numberOfStudents} students at the ${this.level} school level.)
}

static pickSubstituteTeacher(substituteTeachers) {
const randomNumber = math.floor(math.random()*substituteTeachers.length);
return substituteTeachers[randomNumber];
}
}

class PrimarySchool extends School {
constructor(name, numberOfStudents, pickupPolicy) {
super(name, ‘primary’, numberOfStudents);
this._pickupPolicy = pickupPolicy;
}

get pickupPolicy() {
return this._pickupPolicy;
}
}

class MiddleSchool extends School {
constructor(name) {
super(name);
}
}

class HighSchool extends School {
constructor(name, numberOfStudents, sportsTeams) {
super(name, ‘high’, numberOfStudents);
this._sportsTeams = sportsTeams;
}

get sportsTeams() {
return this._sportsTeams;
}
}

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.’);
lorraineHansbury.quickFacts();
lorraineHansbury.pickSubstituteTeacher([‘Jamal Crawford’, ‘Lou Williams’, ‘J. R. Smith’, https://gist.github.com/5f71c387de147c9c15b545ab356912e0

Hello All,

May i know why i can’t do

lorraineHansbury.pickSubstituteTeacher([‘Jamal Crawford’, ‘Lou Williams’, ‘J. R. Smith’, ‘James Harden’, ‘Jason Terry’, ‘Manu Ginobli’]);

or

School.pickSubstituteTeacher([‘Jamal Crawford’, ‘Lou Williams’, ‘J. R. Smith’, ‘James Harden’, ‘Jason Terry’, ‘Manu Ginobli’]);

?

and also

line 38-42

class PrimarySchool extends School {
constructor(name, numberOfStudents, pickupPolicy) {
super(name, ‘primary’, numberOfStudents);
this._pickupPolicy = pickupPolicy;
}

So this is the first time i see more than one argument in the subclass constructor, so can i sum up, whatever set as changeable date like numberOfStudents, should be put in constructor arguement, and the one as “set” as primary in class PrimarySchool should be put in the super? And why numberOfStudents appear in both?

and for line 71

console.log(alSmith.sportsTeams);

why it is a problem to say

console.log(alSmith.sportsTeams();

Thanks!


#2

Hey, I am having a bit of a struggle to read your code. Could you use the code formatter to post your code?

Such as this:

class School {
     constructor(name, level, numberOfStudents) {
         this._name = name;
         this._level = level;
         this._numberOfStudents = numberOfStudents;
    }

    get name() {
        return this._name;
    }

    get level() {
        return this._level;
    }

#3

@chloeoscar

set numberOfStudent(num) {

Student is missing an s -> Students

I agree, though there’s a github link further down in the post, the code is readable there.
Maybe it’s too much putting it inside the post and the github link? :slight_smile:


#4

Nvm i noticed the link to your export. Thx ;). But for future posts, the code format on the forums is awesome :wink:


#5

This doesn’t work, remove static
static pickSubstituteTeacher(substituteTeachers) {

math is not defined, use Math instead of math (capital M):
const randomNumber = math.floor(math.random()*substituteTeachers.length);

Seems to be working with these changes + the missing s I mentioned above.


#6

The static setting on that function is a required part of the lesson, though. The correct way to call it is as the OP identifies:


#7

I see, my bad, I was simply looking at the thrown errors in the console of my test environment, it told me pickSubstituteTeacher is not a function so I figured the static keyword was the problem. Removing it fixed it for me.

thanks for the headsup! :slight_smile:


#8

Yes. Anything that you’re including when doing new Object() should be passed to the constructor.

Yes and in this case only in the Super(), because this property value is being set directly inside the class, and is not being passed via new Object().

(As does name).
That’s because those properties are created by the constructor of the superclass, School.

You pass them all into the constructor, as per above comment. But any property that is defined in the superclass must be passed also into Super().
Otherwise the property will not be created in the child class, and thus will not be present in the objects you create with it.