@katerniko: Hi, although this does work, I think @miskolin8 and I too are confused since the Rectangle function which has the calcArea method nested inside it has already been closed (with the curly brace and semi-colon on line 6). So I believe (or I do certainly) that defining the new calcPerimeter method should be being added as if it is an additional insertion once the original version of the Rectangle function was initialised. So using 'this.' will create a problem since it is not actually inside anything as yet. My code looks as follows, though doesn't work:
It makes it a lot easier to read as it keeps formatting and according to your tries it is also less work to do
@c-daniels You could later add a method to a constructor using the prototype property but as far as I can see this comes in a later exercise. So from what I can see @katerniko 's approach of having 4 properties: height, width and the 2 methods calcPerimeter and calcArea seems to be the way to go.
@miskolin8 The advantage of this over using a regular function and probably the problem in your code is that you don't have to care about height in width as they are already part of the object. Meaning if you have a function you'd need parameters for height and width so that you can compute the perimeter and in the function call you'd need something like calcPerimeter(rex.height, rex.width) with rex.height and rex.width as arguments. But with everything wrapped up in one object you just need to call rex.calcPerimeter() because height and width are already part of rex and can easily be accessed from inside the object via this.height and this.width. I hope this makes sense and is not too confusing otherwise I'll give it another try.
So as you can see the problem in your code is that the calcPerimeter method has parameters but you don't give it arguments. What you do is you update the internal properties width and height with the received arguments. Unfortunately as you don't have arguments passed to the function width and height are set to undefined which leads to 2*undefined+2*undefined -> NaN.
@c-daniels Hope the previous part also answered your question if not maybe open another topic as this seems to be a different problem, or isn't it @miskolin8?
I actually made an error in my orignal query. And you would have struggled to spot it due to the way the code was on the OP's question. In actual fact the curly brace and semi-colon on line 6 is the closing (not sure of the correct terminology) of the calcArea method WITHIN the Rectangle function - so my apologies.
By the way, @haxor789 is this Rectangle function still considered an Object since it has no assignment included?? Not sure if I remember a function acting as an object on previous tutorials.
Thanks for the note about the 'prototype' which will be covered later. I see it very frequently online and was wondering what it was for?
Ok rereading your post it makes a lot more sense, I just got confused because the OPs code was correct on this, sry.