# 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 there, 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.

#28

Thansk for this! I got an overview of the classes and objects basic principles. At least for a while I know what they are and how to set tup them. I try to implement this knowledge in my future javascript studies- I have decided to go for MERN. This means I need to have a good knowledge of Javasript.

#29

Start small, always, and gradually ease forward from there. Never write big off the scratch.

``````class Shape {
constructor () {
this.s = [1, 1, 1]
}
is_regular () {
x = new Set(this.s)
return x.size === 1;
}
}

x = new Shape();
console.log(x instanceof Shape);
console.log(x.is_regular());
``````

We could go for instant gratification and fancy up the output. Template literals will work good for that. Let’s do it. Start with this,

``````console.log(`\${x instanceof Shape}`);
console.log(`\${x.is_regular()}`);
``````

Then build whatever text you want around it.

``````console.log(`This is a shape. \${x instanceof Shape}`);
console.log(`It is regular... \${x.is_regular()}`);
``````
``````This is a shape. true
It is regular... true
``````

That doesn’t even begin to help us go anywhere with this new class. Can you think of ways to subclass the heck out of this one?

While it is small, we can explore otherwise pointless (but meaningful) aspects that may or may not apply going forward. Consider a small addition to our class…

``````class Shape {
constructor () {
this.s = [1, 1, 1, 1, 1, 1, 1]
}
is_regular () {
x = new Set(this.s)
return x.size === 1;
}
is_a () {
return ['point','line','angle','triangle','quadrilateral','pentagon',][this.s.length] || `polygon of \${this.s.length} sides`
}
}

x = new Shape();
console.log(`This is a shape. \${x instanceof Shape}`);
console.log(`This shape is a, \${x.is_a()}`);
console.log(`It is regular... \${x.is_regular()}`);
``````
``````This is a shape. true
This shape is a, polygon of 7 sides
It is regular... true
``````

#30

Honestly I don’t get half of this code. I kinda do and I kinda don’t.

is_regular () {
x = new Set(this.s)
return x.size === 1;
}

x = new Shape();
console.log(x instanceof Shape);

is_a () {
return [‘point’,‘line’,‘angle’,‘triangle’,‘quadrilateral’,‘pentagon’,][this.s.length] || `polygon of \${this.s.length} sides`
}

#31

This is why we always start small, and make sure we have our bearings going forward. A regular polygon is one where all the sides are equal length.

``````x = new Set(this.s)
``````

`this.s` in our class is an array. When we call the Set constructor on a list, the resulting set will have no duplicated values. Sets consist of only unique values.

``````return x.size === 1    // will be true if regular
``````

If all the sides are equal, the resulting set will have only one value., hence a size of 1.

``````x = new Shape();
console.log(x instanceof Shape);
``````

Above we create a new instance of the Shape class and confirm it in the next line.

``````is_a () {
return [‘point’,‘line’,‘angle’,‘triangle’,‘quadrilateral’,‘pentagon’][this.s.length] || `polygon of \${this.s.length} sides`
}
``````

`this.s.length` is the number of sides. It will correspond with a noun from the list, if there is small enough index. If not, then OR returns the latter value.

#32

What’s the type of expression you used in is_a method? Or what’s the name of the approach you implemented? Usually it would require an if statement, but in your case it’s somesort of js functionality that has included the if statement within.

#33

U could make several subclasses: like round, triangle, rectangle, hexagon… Then you could add an attribute like nr of sides. Then you can define all the sides equally or the code cask the numbers for each kind of side.

#34

It’s a boolean, logical OR.

``````return False || False     =>  False

return False || "other"   =>  other
``````

You’ll need to brush up on logical operators to fully understand this.

That will come in time. First we need to be sure to cover all the aspects they have in common so their methods can be written to the Shape class and inherited by the derived classes.

For now I have sides all contained in an array for simplicity.

Edit. JS does not support tuples. My bad.

#35

Ok I am so noob in programming I didn’t even know anything about tuples. Seems that js doesn’t have direct support for this, at least not in es6. Dont know about es7 and es8.

Yeah noticed your edit. already had googled for tuple’s. So how to handle it in js? https://medium.com/@wpcarro/tuples-in-javascript-57ede9b1c9d2

Probably an array or object literal or a class declaration in which u declare the sided as their line up is predefined in the constructor.

#36

Getting my languages crossed. JS does not support tuples. Thanks for the article link.

#37

What are in besides js? Python?

JS the 2nd language I am trying to learn. The 1st one was c# and I got tired of it, js is more closer to my nature. But it’s hard to understand it still, as I mainly have introductory course done. Did the majority of it on my 2nd account and the last part in here as this is the paid pro account now. Probably will be using that account for a while as I am trying to learn react soon.

What about practical learning possibilities of js. I am tired of these interactive excerises. Would like to do something with projects that are made for learning. Just write the code and mess with it, until you get it to work.

#38

Yes, Python supports tuples. If we pass a tuple into a class constructor, we can unpack it…

``````>>> class Shape(object):
def __init__(self, sides):
self.a, self.b, self.c = sides

>>> triangle = Shape((3, 4, 5))
>>> print (triangle.c)
5
>>>
``````

#39

ok. I see.

About the boolean thing, was it a ternary operator perhaps?

#40

No, not a ternary. Pure boolean logic.