# 24.dont know why it isn't working-.-

#1

function Rectangle(height, width) {
_ this.height = height;_
_ this.width = width;_
_ this.calcArea = function() {_
_ return this.height * this.width;_
_ };_
_ // put our perimeter function here!_
_ this.calcPerimeter=function(height,width){_
_ this.height=height;_
_ this.width=width;_
_ return (2*height + 2*width);_
_ }_

_ }_
_ _

_ _
var rex = new Rectangle(7,3);
var area = rex.calcArea();
var perimeter = rex.calcPerimeter();

i dont know why but this isnt working-.-. And can someone please explain to me why do i have to use this.calcPerimeter=function() why cant i just use var calcPerimeter= function()

Thanks:D

#2

function Rectangle(height, width) {
this.height = height;
this.width = width;
this.calcArea = function() {
return this.height * this.width;
}

// put our perimeter function here!
this.calcPerimeter = function() {
return 2 * this.height + 2 * this.width;
}
};

This one helped,you need to put the code inside the "{};"

#3

@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:

``````Rectangle.calcPerimeter = function() {
return 2*this.height + 2*this.width;
};``````

@haxor789: You helped me out previously, could you offer any advise on this?

#4

Ok first of all @miskolin8 and @katerniko Please have a look at this on how to format your code:

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?

#6

Thanks @haxor789 for your speedy response.

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?

#7

Ok rereading your post it makes a lot more sense, I just got confused because the OPs code was correct on this, sry.

As far as I know apart from primitive data types almost everything in JavaScript is in the end an Object . So even regular functions are objects but it doesn't make sense to introduce them as Objects if you're not used to the concept of object yet, does it? And in case of the constructor function as said it is an object but the point of it is mainly to be used as a constructor for new Objects so referring to it as an Object might have been confusing, sry.