Setter in javascript project "School Catalogue" not doing anything please help


#1

#2

This,

quickFacts() {
    console.log(`${this._name} educates ${this._numberOfStudents} at the ${this._level} school level.`);
}

Can be written to use the accessor (getter)…

quickFacts() {
    console.log(`${this.name} educates ${this.numberOfStudents} at the ${this.level} school level.`);
}

Please post the URL for this project. Thanks.


#3

that’s something I’m still confused when i can use the accessor(getter).


#4

Any time you wish to poll an attribute, such as above. Only the getter should be using the direct property name, _name, etc.

We still need a link to the exercise so we can examine the instructions and test your code.


#5

how do i paste the link? just copy the url from the browser?


#6

So, i can use immediately after the getter inside the object as well as outside it using the correct syntax?


#7

https://www.codecademy.com/courses/learn-javascript-classes/projects/school-catalog?course_redirect=introduction-to-javascript


#8

Yes. Inside the object (object context) we write it with this.name, and outside we write it with the instance name, school.name.

Aside

This,

  set numberOfStudents(newNumberOfStudents) {
    if(typeof newNumberOfStudents === 'number') {
      this._numberOfStudents = newNumberOfStudents;
    } else {
      console.log('Invalid input: numberOfStudents must be set to a Number.')
    }
}

can be simplified, thus…

  set numberOfStudents(number) {
    if (+number) {
      this._numberOfStudents = number;
    } else {
      console.log('Invalid input: numberOfStudents must be set to a Number.');
    }
  }

The unary operator will attempt to cast the value to a number. If it cannot be cast, the new value will be NaN which casts to false in a boolean expression or conditional (so does not raise an exception). Note that 0 will also log out the input error.

This is not a technical concern with your code, just something I thought worth sharing.

+n  =>  Number(n)

Both will be NaN if n cannot be cast to a number. Note that,

n = 'a'
+n == Number(n)                    => false
+n === Number(n)                   => false

This should make great extra reading if you explore NaN and isNaN(), as well as the unary operator and Number() class. We can see it is a little bit different from typeof, but seems more mathematical in the logic.
NaN cannot be equated (compared) to NaN. JavaScript has a special function for testing numbers called, isNaN()

isNaN(+n)  == isNaN(Number(n))     => true
isNaN(+n)  === isNaN(Number(n))    => true

Extra notes

  1. ‘+n’ and ‘Number(n)’ do not change the value referenced by n. They coerce the value to a number type or NaN. n keeps its value.
  2. In this code the expression is used in a conditional, so not assigned. We are only concerned with state, and nothing else. The value itself is of no consequence. It’s a number or it’s not a number.
  3. JavaScript is loosely typed and has many instances of coercion. Even though we are taught to match data types in our logic, and most languages force this upon us, it is important to learn how to use loosely typed data objects, and when it’s sort of okay to use coercion.

#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.