School catalogue

You’re welcome. There is still work to be done if you ever wish to come back to the project (or continue on, now).

For instance, exam scores to form a database from which to calculate exam class average, term class average, or student averages (an extra feature).

Above, how might we allow a list of scores in one go, rather than one at a time?

Can the attributes and methods be inherited from the parent class? Answer, yes, so there would be one distribution of the codebase. Individual subclasses can override if need be.

So now the School class will carry the load. We add in a property that I called meanTestScores (to lose three letters from the name).

The multiple entries problem I solved by having two methods, rather than a bunch of logic in one.

  get meanTestScores () {
    return this._meanTestScores;
  }
  addTestScore (score) {
    this._meanTestScores.push(score)
  }
  addTestScores (scores) {
    this._meanTestScores.push(...scores)
  }
  classAverage () { 
    return this.meanTestScores.reduce((a, b) => a + b) / this.meanTestScores.length
  }

Lots ways this project can go.

1 Like

Hi, I apperciate your feedback, it is especially usefull and motivating for me at the moment.

As you probably noticed, I’ve stuck over an elementary things. That’s because I am distracted doing this exercise along with going through learning process of theory materials of this course. It’s important for me at the moment to create a solid base knowledge as far as it possible for beginner to aproach tasks more or less adequately, as well as mixing it up with practice.

I like your idea to go further in this regards, to extend evaluation of tests scores and to improve code in such way, forthermore, initially i had some thoughts of extending this exercise in this way.

I’ll be back in short time, after absorbing portion of general theory , with some proposal of mentioned above code development :slight_smile: .

1 Like

sorry sorry i guess i had not mastered the format thing in the forum…sorry!

may i know why not using statics here?

ok cool, so why level doesn’t need to be included?

No need to apologize ;). Just a little advice for a future post.

Hi, can you have a look at this issue pls.

Thanks.

Does anyone have the full code solution? My syntax is off but I can’t find where for the life of me.

also, can someone explain this issue?

"Maximum call stack size exceeded at PrimarySchool.get name [as name] "

thanks

Hello @laurafitzpatrick123.

This error indicates an infinite loop or circular reference. I don’t think you’ll have any luck getting the “full code solution” on the forum. There are many of us who are glad to help, if you’ll post your code, but giving out solutions to exercises is a no, no. If you would like help, and choose to post your code, please click the </> button in the menu bar first. Then paste your code in the space indicated. It will preserve your formatting, and make it possible for us to copy and run your code ourselves to help you work through the issues.

Please can someone help me understand how to create a SchoolCatalog that holds the collection of schools under the extra work at step 19 so you can display all the different schools under one class?

I just can’t figure it out. I don’t know if the info is stored within the class itself or if it can only be accessed via the instances? Would it require a new method within the SchoolCatalog that would grab the info from the other classes?

When creating a new SchoolCatalog instance do you directly reference the classes themselves or just the other instances you created?

I feel there is a simple solution but it’s completely evading me, I’m absolutely stumped. TIA.

3 Likes

I’m curious as to why this project instructed us to generate the random number to be one less than the substituteTeachers array, and also what is the purpose of the console.log in the sportsTeam getter?

Thanks for any help.

If you had the following array of persons:

> const subs = ["Mr. Smith", "Mrs. Smith", "Mr. Brown", "Mrs. Brown"]

What’s the length of the array?

> console.log(subs.length)
4

What is the index of the last element?

> console.log(subs.indexOf("Mrs. Brown"))
3

If we didn’t specify that the random number to be used as the index for selecting a random substitute teacher had to be in the range of 0 to 3 (the maximum possible is one less than the length of the array) we could end up with an index of 4. If we try to select subs[4] what do we get?

> console.log(subs[4])
undefined

While the students might enjoy having undefined for a substitute teacher, I don’t think the school administrators or the parents would approve. :wink:

The purpose of console.log() is always the same. Print something to the console. This project allows you some freedoms. You click off the steps as you complete them. When I did this one, I did not use console.log() in the getter. It seemed inconsistent to me, so I returned a string that contained the sports teams. In the end it doesn’t really matter that much if the getter prints the teams, or returns a string for the caller to print (ie. console.log(someSchool.sportsTeams)).

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.numberOfStudent;

;

}

set numberOfStudent(value) {

if (value.isNaN(newNumberOfStudents)) {

  console.log("Invalid input: numberOfStudents must be set to a Number.");

} else {

  this._numberOfStudents = value;

}

}

quickFacts() {

console.log(

  `${this.name} educates ${this.numberOfStudents} students at the ${this.level}school level.`

);

}

static pickSubstituteTeacher(substituteTeachers) {

const randInt = Math.floor(Math.random() * substituteTeachers.length);

return substituteTeachers[randInt];

}

}

class PrimarySchool extends School {

constructor(name, numberOfStudents, pickupPolicy) {

super(name, "primary", numberOfStudents);

this._pickupPolicy = pickupPolicy;

}

get pickupPolicy() {

return this._pickupPolicy;

}

}

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();

this is crazy am getting undefined and i dont understand why

Look closely at you numberOfStudents getter.:slightly_smiling_face:

Can someone please explain why, when making child classes (e.g. PrimarySchool), we don’t pass level as an argument in the constructor()? I am very confused as to why it is omitted from the arguments, but then right below it, its value of “primary” is passed through super().

Here is what the suggested code looks like:

class PrimarySchool extends School {
  constructor(name, numberOfStudents, pickupPolicy) {
    super(name, 'primary', numberOfStudents);
    this._pickUpPolicy = pickUpPolicy;
  }

There would only be a level property if the aim is to override the parent class property. That is not the case, here. The child class has simply to pass the argument to the parent constructor. It will inherit the level property, along with the other two.

I am currently working through the project being discussed here. I have gotten everything to work except for one bit of code, and I am struggling to understand why it isn’t working.

class School { constructor(name, level, numberOfStudents) { this._name = name; this._level = level; this._numberOfStudents = numberOfStudents; } static pickSubstituteTeacher(substituteTeachers) { substituteTeachers = []; let randomNum = Math.floor(Math.random() * substituteTeachers.length); return substituteTeachers[randomNum]; } }; console.log(School.pickSubstituteTeacher('Jamal Crawford', 'Lou Williams', 'J. R. Smith', 'James Harden', 'Jason Terry', 'Manu Ginobli')); //undefined is being logged to the console

If I input the teacher names directly into the array in my method, and then call the method and console.log it, it works just fine.

class School { constructor(name, level, numberOfStudents) { this._name = name; this._level = level; this._numberOfStudents = numberOfStudents; } static pickSubstituteTeacher(substituteTeachers) { substituteTeachers = ['Jamal Crawford', 'Lou Williams', 'J. R. Smith', 'James Harden', 'Jason Terry', 'Manu Ginobli']; let randomNum = Math.floor(Math.random() * substituteTeachers.length); return substituteTeachers[randomNum]; } }; console.log(School.pickSubstituteTeacher()); //logs a randomly chosen name from the substituteTeachers array

I can’t figure out why when I call the method and pass the teacher names as the argument, it doesn’t seem to be injecting those names into the substituteTeachers array inside my method. I have also tried wrapping those names in [brackets], and I still get undefined logged to the console.

note - This is not all of the code in the project. I have only pasted the code I think is relevant to my question, and the code that is necessary to make sense of it (i.e. the static method doesn’t make sense if I don’t specify that it is inside of a class).

here is a link to the project (though I’m not sure if all my code is going to be populated in the console)
School Catalogue Project

Thank you in advance

Ok, through trial and error I’ve gotten the code working. But I’m not really sure why this syntax works. Perhaps someone could explain it to me?

Apparently I was not supposed to define the substituteTeachers property as an array within the method, and I had to wrap all the names in [brackets] when I called the method.

  1. why was my code not working when I had substituteTeachers = in the method? Were the names being properly passed in as an argument, and then the method was reassigning the array to an empty array? That’s what I’m assuming was happening.

  2. Why does the array that is passed as an argument have to be wrapped in brackets, instead of just being able to specify within the method that I want the parameter/argument to be an array?

class School { constructor(name, level, numberOfStudents) { this._name = name; this._level = level; this._numberOfStudents = numberOfStudents; } static pickSubstituteTeacher(substituteTeachers) { let randomNum = Math.floor(Math.random() * substituteTeachers.length); return substituteTeachers[randomNum]; } }; console.log(School.pickSubstituteTeacher(['Jamal Crawford', 'Lou Williams', 'J. R. Smith', 'James Harden', 'Jason Terry', 'Manu Ginobli']));

If you had
substituteTeachers = []
in the method, then you’d replace what the variable substituteTeachers belongs to [“references”] with an empty array. Which would be a problem since there’s nothing in an empty array, and there would no no substitute teachers to choose from.

The last line you have is

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

The argument there has the [ and ] because its an array.
You could have done that separately:

const list_of_subs = ['Jamal Crawford', 'Lou Williams', 'J. R. Smith', 'James Harden', 'Jason Terry', 'Manu Ginobli']; 
console.log(School.pickSubstituteTeacher(list_of_subs));

as long as the argument for that method is an array.

2 Likes