School Catalogue : Step 14


#1

Hello, I’m working on the School Catalogue Lesson.
I’m trying to create my first instance (step 14) and mah code be barken are me. :slight_smile:

// step 1
class School {
  // step 2
  constructor(primary, middle, high){
    // step 3
    this._name = name;
    this._level = level;
    this._numberOfStudents = number;
  }
  // step 4
  get name () {
    return this._name;
  }
  // step 4
  get level () {
    return this._level;
  }
  // step 4
  get numberOfStudents () {
  	return this._numberOfStudents;
  }
  	// step 5
  set numberOfStudents (newNumberOfStudents){
    if (typeof newNumberOfStudents == 'number'){
    	this._numberOfStudents = newNumberOfStudents;
    } else {
      console.log('Invalid input: numberOfStudents must be set to a Number.'); 
    }
  }
  set numberOfStudents(num){
    this._numberOfStudents = num;
  }
  // step 6
  quickFacts(){
    //console.log('Step 6 was tricky for me.  Not sure how to call the objects requested.');
    console.log(`${school.name}: educates ${school.numberOfStudents} students at the ${school.level} school level.`)  // do i need 'this'?
  }
  // step 7
    /*Static methods are not directly accessible using the this keyword from non-static methods. You need to call them using the class name: CLASSNAME.STATIC_METHOD_NAME() or by calling the method as a property of the constructor: this.constructor.STATIC_METHOD_NAME(). https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce?v=example 
    */
  static pickSubstituteTeacher(substituteTeachers){
    let subTeachIndex = Math.floor(substituteTeachers.length * Math.random());
    // not sure what this function is for
    return substituteTeachers[subTeachIndex];
  }
}
// step 8
class PrimarySchool extends School {
  // step 9
  constructor(name, numberOfStudents, pickupPolicy){
    // step 10
    // must pass any arguments that the parent constructor() uses
    super(name, 'primary', numberOfStudents);
    // step 11
    this._pickupPolicy = pickupPolicy // 'string'
  }
  // step 12
  get pickupPolicy (){
    return this._pickupPolicy;
  }
}

// step 13 
class High extends School {
  constructor (name, numberOfStudents, sportsTeams){
    super(name, 'high', numberOfStudents);
    this._sportsTeams = sportsTeams; // array of 'strings'
  }
  get sportsTeams () { 
  	return this._sportsTeams;
  }
}
// step 14
const lorraineHansbury = new PrimarySchool('Lorraine Hansbury', 'primary', 'Students must be picked up by a parent, guardian, or a family member over the age of 13.');

When i click the [save] button, it kicks out the following in the console window:

/home/ccuser/workspace/learn-javascript-classes-school-catalog/app.js:6
    this._name = name;
                 ^

ReferenceError: name is not defined
    at PrimarySchool.School (/home/ccuser/workspace/learn-javascript-classes-school-catalog/app.js:6:18)
    at PrimarySchool (/home/ccuser/workspace/learn-javascript-classes-school-catalog/app.js:53:5)
    at Object.<anonymous> (/home/ccuser/workspace/learn-javascript-classes-school-catalog/app.js:74:26)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:427:7)

That message “ReferenceError: name is not defined” has me puzzled. I’ve compared it to the examples in the earlier lessons and I can’t find the enigma.


#2

I’m thinking that the problem could be in // step 13.

// step 13
class High extends School {
constructor (name, numberOfStudents, sportsTeams){
super(name, ‘high’, numberOfStudents);
this._sportsTeams = sportsTeams; // array of ‘strings’
}
get sportsTeams () {
return this._sportsTeams;
}
}

#3

It says the error is one line 6…

  constructor(primary, middle, high){
    // step 3
    this._name = name;
    this._level = level;
    this._numberOfStudents = number;
  }

#4

Roy, you are a trooper! Thank you for responding to my posts.

Yes, I’m aware of that line 6 error but, up to step 14 it would save just fine. I got the error after adding step 14. If it is line 6, I can’t see the problem. I’m a bit stumped with this at the moment…

perhaps it has something to do with my constructor or super reference in // Step 13?

constructor (name, numberOfStudents, sportsTeams){
super(name, ‘high’, numberOfStudents);

#5

Examine your constructor, above. The parameters do not match the variables being assigned to the properties. name, level and number are undefined.


#6

I think you are right. I need to ponder this. I miss understood step 2.

changing step 2 to:  constructor(name, level, numberOfStudents){ ...

Now giving an error on line 8 but, I think I have a problem with the constructor in // step 9. I’m squeezing my brain … :slight_smile: I’m getting close. I can feel it!


#7

Let’s see what you now have…


#8

I have to say that i think the tutorial does a lousy job explaining the significance of super’s and constructor’s, their relationship and the significance/purpose of the parameters passed within them.

  // step 2
  constructor(name, level, numberOfStudents){
...
// step 8
class PrimarySchool extends School {
// step 9
  constructor(name, level, pickUpStudents){
    // step 10
    // must pass any arguments that the parent constructor() uses
    super(name, 'primary', numberOfStudents);
...
// step 14
const lorraineHansbury = new PrimarySchool('Lorraine Hansbury', 'primary', 'Students must be picked up by a parent, guardian, or a family member over the age of 13.');

Error:

/home/ccuser/workspace/learn-javascript-classes-school-catalog/app.js:53
    super(name, 'primary', numberOfStudents);
                           ^

ReferenceError: numberOfStudents is not defined
    at PrimarySchool (/home/ccuser/workspace/learn-javascript-classes-school-catalog/app.js:53:28)
    at Object.<anonymous> (/home/ccuser/workspace/learn-javascript-classes-school-catalog/app.js:74:26)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:427:7)
    at startup (bootstrap_node.js:151:9)

#9
class Foo {
  constructor(name, size) {
    this.name = name;
    this.size = size;
  }
}
class Bar extends Foo {
  constructor(name, size, count) {
    super(name, size);
    this.count = count;
  }
}

Notice above that Bar only has one localized instance variable, but inherits the name and size properties from Foo. By using the super function we instantiate a new Foo class with the name and size variables so that Bar can inherit them and other methods from the parent class.

If we instantiate Bar with all three properties, name and size will override the parent properties. However, and this will need testing, if there are methods in Foo that depend on the local properties, they might not work in the extended class. Using super keeps all the channels open and the data declared in all necessary locations.

One important note, super is always the first statement inside the constructor and should never follow other properties.