I can not understand why "Uncaught RangeError: Maximum call stack size exceeded" appears. I am trying to solve:
The problem:
Create an object Polygon for instantiating a new Polygon that will output to the console a text based on how many segments the polygon is composed by. The polygon has one property segments
that stores this value and can be accessed via its getSegments()
function.
By default it has 3 segments.
If the segments are strictly less than 3, Polygon will set them to 3 and log an error saying "A polygon need to have 3 segments, it
will be set to 3".
The cases are:
1. If segments are undefined the polygon name is triangle
2. If segments are <3 the polygon name is set to triangle
3. If segments are 3 the polygon name is set to triangle
4. If segments are 4 the polygon name is set to quadrilateral
5. If segments are 5 the polygon name is set to pentagon
6. If segments are 6 the polygon name is set to hexagon
What I tried to do?
I tried creating class Polygon and then function getSegments(segments) to change the segments property of the new object depending on the segments prop to be passed along the conditionals, and in the situation where i am calling the script externally from index.html page, it works. But when I try to paste the code into browser console, and in the Node.js environment i am getting "Uncaught RangeError: Maximum call stack size exceeded" error. I haven't encountered this kind of error before, so if anybody could see what I did wrong there, or know how to explain to me how to avoid that in the future, I would very much be grateful.
class Polygon {
constructor () {
this.segments = getSegments();
}
}
function getSegments(segments) {
this.segments = segments;
if (segments < 3) {
polygon = new Polygon(segments)
console.log ('A polygon need to have at least 3
segments. It will be set to 3.')
polygon.segments = 3
} else if (segments === 3) {
polygon = new Polygon(segments)
console.log ('The polygon name is triangle')
} else if (segments === 4) {
polygon = new Polygon(segments)
console.log ('The polygon name is quadrilateral')
} else if (segments === 5) {
polygon = new Polygon(segments)
console.log ('The polygon name is pentagon')
} else if (segments === 6) {
polygon = new Polygon(segments)
console.log ('The polygon name is hexagon')
} else if (typeof segments == 'undefined') {
polygon = new Polygon(3)
console.log ('The polygon name is triangle')
}
}
Results:
I expected output like: getSegments(4)
//-> polygon object with segments property, it's value 4 and console.log text The polygon name is quadrilateral.
Actual output instead is: Uncaught RangeError: Maximum call stack size exceeded.
Thank you in advance for all your advice's and help.
Solution
Actually problem was in this block of code:
`class Polygon {
constructor () {
this.segments = getSegments();
}
}`
line of code that needs to be replaced is:
`this.segments = segments`and with constructor we need to pass segments like this `constructor (segments)`, and the error is resolved.