School catalogue

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!

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;
    }

@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:

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

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.

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:

1 Like

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:

1 Like

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.

2 Likes

Can somebody tell me pls, why this static method I can’t log within created objects (instances). Thanks.

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

when i log in >

const subTeacher = School.pickSubstituteTeacher([‘Jamal Crawford’, ‘Lou Williams’, ‘J.R. Smith’, ‘James Harden’, ‘Jason Terry’, ‘Manu Genobli’]);
console.log(School.substituteTeachers);

the output is undefined.

You’ve assigned the return value of your static functionpickSubstituteTeacher() to a variable named subTeacher. If you want to log the results of the function contained in the subTeacher variable to the console, just log the variable console.log(subTeacher).

You could also log the return value of the function directly without setting it equal to a variable first:

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

console.log(School.substituteTeachers); returns undefined because substituteTeachers isn’t a property of the School class. pickSubstituteTeacher is a static method of the School class, but School.substituteTeachers doesn’t exist, and is therefore ‘undefined’.

Hope this helps.

2 Likes

Hope so. Many thanks for a comprehensive answer.

// Hi, could you please have a look at the next issue i’ve got on that exercise.
Another property has been added to subclass, then method to get an average value. So that is >

this._averageTestScores = ;

getAverageTestScore () {return this._averageTestScores.reduce((a, b) => {a + b}) / this._averageTestScores.length;}

// and

addScore(getAverageTestScore) {
this._averageTestScores.push(getAverageTestScore);
}

// then under the created object script a:

lorraineHan.averageTestScore;
lorraineHan.addScore(9.0);

lorraineHan.addScore(9.1);
console.log(lorraineHan.averageTestScores);

// printed output is

[ 9, 9.9, 8.9, 9.1 ] // but not any average value.

tnx a lot for your time!

I can see a few issues with your code, but it would be much easier for me to help you if you could paste the code using blockquote, or post a screenshot of your code. Thanks!

That line is not doing anything. We need to invoke the method.

lorraineHan.averageTestScore();

Question

Is averageTestScores an attribute of the class instance? Or is the only set attribute testScores?


Aside

I’m amazed that a thread can get this long and still nobody has thought to post a link to the actual exercise. Where is the context, people?

https://gist.github.com/3bf0944cff64c2c196abc306c007bd05

Hi, i didn’t post entire code of this exercise just not to overload comunication with unnecessary information, though initially intended to past it whole. Might be not able yet to consider quickly abt relevancy of whole snippet. I’am sorry.

A gist is okay for code, but we could still make use of the lesson link for verification and testing (and context).

do you mean link to Introduction to JavaScript, Lern JavaScript Classes, School Catalog free form project?

Yes, the one this topic applies to.

https://www.codecademy.com/courses/introduction-to-javascript/projects/school-catalog

1 Like