Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created April 21, 2020 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecademydev/a52d9fd60f70acda15ffdecec577ed32 to your computer and use it in GitHub Desktop.
Save codecademydev/a52d9fd60f70acda15ffdecec577ed32 to your computer and use it in GitHub Desktop.
Codecademy export
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 numberOfStudents(newNumberOfStudents) {
if(typeof newNumberOfStudents === 'number') {
this._numberOfStudents = newNumberOfStudents;
} 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(substituteTeachers.length * Math.random());
return substituteTeachers[randomNumber];
}
}
class PrimarySchool extends School {
constructor(name, numberOfStudents, pickUpPolicy) {
//including numberOfStudents because it was set(setter method) as changeable data,
// 'primary is included in super and not in this._level because it's value is set directly in class and not being passed via new class
// 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.
super(name, 'primary', numberOfStudents);
this._pickUpPolicy = pickUpPolicy;
}
get pickUpPolicy() {
return this._pickUpPolicy;
}
}
class HighSchool extends School {
constructor(name, numberOfStudents, sportsTeams) {
super(name, 'high school', 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();
const sub = School.pickSubstituteTeacher(['Jamal Crawford', 'Lou Williams', 'J. R. Smith', 'James Harden', 'Jason Terry', 'Manu Ginobli']);
console.log(School.pickSubstituteTeacher);
const alSmith = new HighSchool('Al E. Smith', 415, ['Baseball', 'Basketball', 'Volleyball', 'Track and Field']);
//call alSmith.sportsTeams without () because it's calling a getter method and getter methods have the same syntax as a property object.property
console.log(alSmith.sportsTeams);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment