School Catalogue - numberOfStudents not working

Everything works. :slight_smile:
I think the lesson (task - Introduction to JavaScript - School Catalogue) should be corrected by CoceCademy.
Thanks again.

1 Like

Fortunately for us, all numbers are technically floats.

That’s not a good thing, and is what makes javascript a bad language.
Everything works.
Forgot a semicolon? Works.
Wrong type? Works.
Not defined? Works.
Wrong number of arguments? Works.

You’re no longer able to tell when something obviously should not work.

1 Like

Are you getting the expected output when you call the .quickFacts() method?

Similarly, isFinite compares a value to a few special floating point values.

So if NaN is to be considered appropriate for testing type, then isFinite should also be considered appropriate because it does the same thing (comparisons to specific special values).

isFinite('stuff')  // test if stuff is a number

The only difference being that isFinite doesn’t have “number” as part of its name, and therefore can’t be mistaken for having something to do with types.

Gave this some thought, and I see exactly what you mean. My solution to using the setter should ‘break’ the code, but it doesn’t. I’ve gone back to my original code in the project, and revised it to avoid these obvious faults. Feedback welcome.

class School {
  constructor(name, level, numberOfStudents) {
    this._name = name
    this._level = level
    this._numberOfStudents = this.validateNumStudents(numberOfStudents)
    this._averageTestScores = []
  get name() {
    return this._name
  get level() {
    return this._level
  get numberOfStudents() {
    return this._numberOfStudents
  set numberOfStudents(num) {
    this._numberOfStudents = this.validateNumStudents(num)
  validateNumStudents(num) {
    if (typeof num != 'number' || num < 0 || num != Math.floor(num)) {
      console.log('Invalid input: numberOfStudents must be set to a whole number greater than zero.')
      return `*Please update. ${num} is invalid.*`
    } else {
      return num
  quickFacts() {
    return `${} educates ${this.numberOfStudents} students at the ${this.level.slice(0,1).toUpperCase() + this.level.substring(1)} School level.`
  addTestScore(score) {
  get averageTestScores() {
    return this._averageTestScores.reduce((a,b)=> a + b) / this._averageTestScores.length
  static pickSubstituteTeacher(teacherArray) {
    return teacherArray[Math.floor(Math.random() * teacherArray.length)]

class Primary extends School {
  constructor(name, numberOfStudents, pickupPolicy, averageTestScores) {
    super(name, 'primary', numberOfStudents)
    this._pickupPolicy = pickupPolicy
  get pickupPolicy() {
    return this._pickupPolicy
  set pickupPolicy(policy) {
    this._pickupPolicy = policy

class Middle extends School {
  constructor(name, numberOfStudents) {
     super(name, 'middle', numberOfStudents)

class High extends School {
  constructor(name, numberOfStudents, sportsTeams) {
    super(name, 'high', numberOfStudents)
    this._sportsTeams = sportsTeams
  get sportsTeams() {
    return `${} has the following sports: ${this._sportsTeams.join(', ')}.`
  addTeam(team) {

const mySchool = new High('Iowa-Grant High School', -186, ['Football', 'Baseball', 'Basketball'])
console.log(mySchool.quickFacts(), "\n")
mySchool.numberOfStudents = 'eleventeen'
console.log(mySchool.quickFacts(), "\n")
mySchool.numberOfStudents = 426
console.log(mySchool.quickFacts(), "\n")
console.log(mySchool.sportsTeams, "\n")

const subTeach = (School.pickSubstituteTeacher(['John Henry', 'Paul Bunyan', 'Santa Claus', 'Mary Poppins']))
console.log(`My substitute is: ${subTeach}`, "\n")

const lorraineHansbury = new Primary('Lorraine Hansbury', 500, 'Students must be picked up by a parent, guardian, or a family member over the age of 13.')
console.log(lorraineHansbury.quickFacts(), "\n")

const alamo = new Middle('Alamo Junior High', -432)
console.log(alamo.quickFacts(), "\n")
alamo.numberOfStudents = 386


Invalid input: numberOfStudents must be set to a whole number greater than zero.
Iowa-Grant High School educates Please update. -186 is invalid. students at the High School level.

Invalid input: numberOfStudents must be set to a whole number greater than zero.
Iowa-Grant High School educates Please update. eleventeen is invalid. students at the High School level.

Iowa-Grant High School educates 426 students at the High School level.

Iowa-Grant High School has the following sports: Football, Baseball, Basketball, Volleyball.

My substitute is: Mary Poppins

Lorraine Hansbury educates 500 students at the Primary School level.

Invalid input: numberOfStudents must be set to a whole number greater than zero.
Alamo Junior High educates Please update. -432 is invalid. students at the Middle School level.

Alamo Junior High educates 386 students at the Middle School level.

I tried refactoring the codecademy provided code for that, but I just ended up removing all the classes because uhm … well what exactly were they doing?
And then what’s left and err.
So I’m not sure there’s any meaningful feedback to conjure up.

Other than maybe use a different language. :sweat_smile:

My problem is I don’t see what problem is being solved. What can be removed, what must stay?

But for validation you mean? Sprinkling validation all over the place is an anti-pattern… and with a decent type system one would immediately be in a better position both in ability to validate and avoid validating

1 Like

What about a simple unary expression?

if (+x) {
    // +x is not NaN

Do you care in the slightest about the NaN value or do you mean to test for something else and if so what?
Either way, that probably does not do whatever it is you meant, unary + doesn’t say anything about testing types does it? So what part of what it promises would you be using?

If you mean “different type from number” then that would be something entirely different, and you’ve already got typeof for that.

Weirdly enough, the unary is not just about numbers.

 > x = []
<- > []
 > +x
<- 0

I get the point.

It also doesn’t promise to return a truthy/falsy value based on some kind of success

1 Like

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