24/33 Introduction to Objects: unable to solve, pls review code


#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()
  {
    return this.height*2 + this.width*2;
  };

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

#2
this.calcArea = function() {
    return this.height * this.width;
};

is a property, so should have a comma, not a semi-colon. Same applies to,

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

However, the current version a JS may not permit a comma on the last property, so it can left off this one.

At the end of the function body the closing brace, }; is missing.


#3

Thank you for that but the calcArea function was already in the editor and the steps says to check that calcArea func and create a similar one by name calcPerimeter.

and as far as the comma is concerned...where it should be again

this.calcArea and this.calcPerimeter....both ends with a semi-colon as it should for a function.


#4

this.calcArea and this.calcPerimeter are methods which is what we call functions when they are members of an object.

    this.calcArea: function() {
        // code
    },
    this.calcPerimeter: function() {
        // code
    }

We only end a function with a semi-colon when it is an anonymous expression in an assignment:

var func = function () {
    // code
};

#5

I think you missed a closing tag for function.

Your code should look like this. :wink:

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

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