Rectangle's Perimeter


#1

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

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

Someone please explain what's wrong with this rectangle's formula?

Thanks!


#2

which exercise is this?
here:

function Rectangle(height, width) {
  this.height = height;
  this.width = width;
  this.calcArea = function() {
      return this.height * this.width;
  };
// forgot to close rectangle object with }

see comment in code

also, why not add both methods (calcPerimeter and calcArea) to the same object? creating the object twice, doesn't seem really DRY (an object can have multiply methods)


#3

How simple is that! Hahaha :sweat_smile:

I had no idea we could add both methods to the same object!

Appreciate your help :slight_smile:


#4

did you figure out how to add the methods to the same object?

Yes, an object can have many methods


#5

yup! figured it out thanks to you :slight_smile: and currently reviewing constructors.


#6

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