Maximum call stack exceeded error


#21

There is still a problem with this method…

   getAverageRating () {  
     let arraySum = function arr () {
       return this._ratings.reduce (function (a,b) {
         return a + b;});
     }
     let arrayAverage = **arraySum**/this._ratings.length; 
     return arrayAverage;
  }
  addRating (newRating) {
    this._ratings.push(newRating);
  }

In your code, arraySum is a function, not a value. It doesn’t need to be a function, though, just a call to reduce() to obtain the sum.

    let arraySum = this.ratings.reduce (function (a,b) {
        return a + b;
    });

or if you want ES6,

    let arraySum = this.ratings.reduce ((a,b) => a + b);
                          ^
                   using getter

Now that we have it nicely simplified, we can write a single return line on the method.

    getAverageRating () {
        return this.ratings.reduce ((a,b) => a + b) / this.ratings.length;
    }

Notice the getter in both operands.

This brings us to addRating

  addRating (newRating) {
    this.ratings = newRating;
  }

Above we call the setter.


#22

Weird. let arraySum = this.ratings.reduce( (a,b) => {a + b;}) / this.ratings.length; Returns undefined.

This: let arraySum = this.ratings.reduce(function (a,b) {
return a + b;
});
let arrayAverage = arraySum/this.ratings.length;
return arrayAverage; however returns numbers in console: 4.666666666666667 and 2.3333333333333335, meaning the main class getAverageMethod is working as constructed . I mean it fills the purpose it was ment for.

I just don’t get why this simplified function call isn’t working properly.

And how to I know, when to call main class paramaters /constructor set up in a child class and when not to mention that speficic valuable is not bound like in means of using the super keyword?


#23

This is what I have for ratings…

  get ratings() {
    return this._ratings;
  }
  set ratings (newRating) {
    this.ratings.push(newRating);
  }
  addRating(rating) {
    this.ratings = rating;
  }
  getAverageRating() {
    return this.ratings.reduce((a,b) => a+b) / this.ratings.length;
  }

The super() method lets us pass arguments to the parent class that both classes have in common. In this case that would be title. Check the parameters of the parent class constructor to see which ones to pass up. If the variable is not in the parameter list, don’t pass it up with (super).

When we instantiate a new instance of a subclass, it invokes an instance of the parent class so that all the attributes are instantiated and can be inherited.


#24

Ok. I think I got it. It’s like connecting the classes of superiority way. I just wonder why do we need extend then, when could bind the classes by super? I see that we kinda have to binders in one class. Why couldn’t javascript syntax just recon one binder not two in the same class as we have now?

Thanks for the help!


#25

Subclasses are a good way to separate objects that could rightly be their own class, but keep them connected under the umbrella of the super class. As we see above, books, records, movies, etc. are all media of one sort or another. If we have only one class to lump them all together under it would be pretty messy once the volume of data is large.

It always helps to separate concerns (attributes and methods). A movie doesn’t have pages, and neither does a record. By having a Book class, we can assign all the attributes needed to properly catalog (curate) a reading library, and any specialized methods that might apply would be contained in that class, only, and not in the parent class. The parent class only needs attributes and methods that are common to all forms of media.


#26

Ok. I kinda generally outline the classes as sets of objects with specific types, attributes, methods and stuff. How do I know what would be best to keep in oneclass and what method or function should go where? Like how to best locate the methods, functions, getters and setters in the classes?


#27

There are no functions, as it were, only methods of a class. Getters and Setters need to be in the class where their associatiated attributes are written. They have an internal connection to their attributes. Methods should be written in the class where they serve the greatest need. If a method is only ever needed by the Book class, then write it their, and not in the parent class. If the method is something that all or most subclasses can or will call upon, then write it in the parent class.