24. Constructor with Methods- Unexpected end of input


#1

Hello,

I need some help with the following challenge:

https://www.codecademy.com/en/courses/spencer-sandbox/3/5?curriculum_id=506324b3a7dffd00020bf661#

I'm getting the error message: unexpected end of input. Can anyone help?

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

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

#2

You never actually close your rectangle function. You put }; which closed out your calcArea function within the rectangle function, but not the rectangle function itself.


#3

I tried this:

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

function calcPerimeter(height, width) {
return this.height*2 + this.width*2;
};

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

Now I get TypeError: rex.calcPerimeter is not a function


#4

That is because your calcPerimiter function should be inside your rectangle function the exact same way calcArea was made.


#5

Got it! Thanks :slight_smile:

Here's the final code:

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

};
};

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


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.