class School{
constructor(name, level, numberOfStudents){
this._name = name
this._level = level
this._numberOfStudents = numberOfStudents
}
//get
get name(){
return this._name
}
get level(){
return this._level
}
get numberOfStudents(){
return this._numberOfStudents
}
//set
set numberOfStudents(numberOfStudents){
if (typeof numberOfStudents === 'num'){
this._numberOfStudents = numberOfStudents
} else {console.log('Invalid input: numberOfStudents must be set to a Number.')}
}
//static
static 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-1);
return substituteTeachers[randomNumber]
}
};//end
///Primary
class Primary extends School{
constructor(name, level, numberOfStudents, pickupPolicy){
super(name, level, numberOfStudents)
this._pickupPolicy = pickupPolicy
}
get pickupPolicy(){
return this._pickupPolicy
}
};
///Middle
class Middle extends School{
constructor(name, level, numberOfStudents){
super(name, level, numberOfStudents)
}
};
///High
class High extends School{
constructor(name, level, numberOfStudents, sportsTeams){
super(name, level, numberOfStudents)
this._sportsTeams = sportsTeams
}
get sportsTeams(){
return this. _sportsTeams
}
};
const lorraineHansbury = new Primary('Lorraine Hansbury', 'primary', 514, 'Students must be picked up by a parent, guardian, or a family member over the age of 13.');
console.log(lorraineHansbury._name)
Primary.quickFacts()
School.pickSubstituteTeacher(['Jamal Crawford', 'Lou Williams', 'J. R. Smith', 'James Harden', 'Jason Terry', 'Manu Ginobli']);
const alSmith = new High('Al E. Smith', 'high', 415, ['Baseball', 'Basketball', 'Volleyball', 'Track and Field']);
console.log(alSmith._sportsTeams)
Thx for the reply.
When i call with: “lorraineHansbury.quickFacts()” the problem is: “TypeError: lorraineHansbury.quickFacts is not a function”
I found the solution:
I remove from “static quickFacts()” the static method and it work, but why?
EDIT
Ok i found.
The exercise say: " Methods : .quickFacts() and .pickSubstituteTeacher() (this is a static method)"
But only the last one is a static method.
//https://www.youtube.com/watch?v=9p2VeKRKZ1Q&feature=emb_title
//main class
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;
}
//setter
set numberOfStudents(value) {
if (value.isNaN()) {
console.log("Invalid input: numberOfStudents must be set to a Number.");
} else {
this._numberOfStudents = value;
}
}
//method
quickFacts() {
console.log(`${this.name} school, educates ${this.numberOfStudents} students at the ${this.level} school level.`);
}
//static method
static pickSubstituteTeacher(substituteTeachers) {
const randInt = Math.floor(Math.random() * substituteTeachers.length);
return substituteTeachers[randInt];
}
}
//subclass PrimarySchool
class PrimarySchool extends School {
constructor(name, numberOfStudends, pickupPolicy) {
super(name, 'primary', numberOfStudends);
this._pickuPolicy = pickupPolicy;
}
get pickupPolicy() {
return this._pickupPolicy;
}
}
//subclass HighSchool
class HighSchool extends School {
constructor(name, numberOfStudends, sportsTeams) {
super(name, 'high', numberOfStudends);
this._sportsTeam = sportsTeams;
}
get sportsTeam() {
return this._sportsTeam;
}
}
// new instance
const lorraineHansbury = new PrimarySchool("Lorraine Hansbury", 512, "Students must be picked up by a parent, guardian, or a family member over the age of 13.");
lorraineHansbury.quickFacts();
/*
const sub = School.pickSubstituteTeacher([
"Jamal Crawford",
"Lou Williams",
"J. R. Smith",
"James Harden",
"Jason Terry",
"Manu Ginobli"
]);
console.log(sub);
*/
// new instance
const alSmith = new HighSchool('Al E. Smith', 415, ["Baseball","Basketball","Volleyball","Track and Field"]);
alSmith.quickFacts();
console.log(alSmith.sportsTeam);
const noNameSchool = new PrimarySchool("No name school", 'AAA', "blablalaa");
noNameSchool.quickFacts();
fixed that issue.
now line 77: returns No name school school, educates AAA students at the primary school level. but should return error, I’ve uses ‘AAA’ instead of integer value. thanks
fixed that issue with this._sportsTeam
now the setter isn’t working line 77:
I’ve created new obj (noNameSchool ) - which is instance of PrimarySchool which extends from main School class
So I’ve pass sting ‘AAA’ as numberOfStudends, and it should return error, because in School class
we have this setter:
set numberOfStudents(value) {
if (value.isNaN()) {
console.log("Invalid input: numberOfStudents must be set to a Number.");
} else {
this._numberOfStudents = value;
}
but the setter is never used anywhere? The constructor uses the underlying property _numberOfStudents directly, it doesn’t use the getter and setter. And JS allows this (shame)
set numberOfStudents(value) {
if (value.isNaN()) {
console.log("Invalid input: numberOfStudents must be set to a Number.");
} else {
this._numberOfStudents = value;
}
this validation, if it still runs, with the string as numberOfStudents ?
Hello, i been doing the Library and the School Catalogue exersices and everything is right but i dont understand the last point… Says “Create class called Catalog that holds all of the Media items in our library.”
I dont know how to do that, is quite confuse.
Here is the code