24/33 constructors with methods

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*.width + 2*height
}

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

Oops, try again. There was a problem with your syntax.

What is wrong? :cry:

If you try to make your indentation match your curly braces - something that should be matched up at all times - then you’ll find that you can’t, because the curly braces are mismatched

A crude but effective enough method to narrow down where the problem is, is to remove about half (save it elsewhere) and run again. If the problem remains then it’s in the half you kept, otherwise it’s in the half that you removed in which case bring some of it back. Repeat until identified.

You can also run it with some other interpreter, typically there’s a line number accompanying error messages.

1 Like

So the rest of my code is correct?

I tried it and removes some curly braces and added but still no positive effect

I was only saying that there’s at least one problem that you shouldn’t be running into just because of keeping the formatting tidy.

There isn’t a } for each { - they’re only allowed in pairs, one at the start of a block and one where that same block ends. If you indent by the curly braces, then the result is that the code doesn’t come back all the way to the left side, some block is still not closed.

If there are other syntax problems, then as suggested earlier - temporarily remove stuff until it is valid syntax, fix the last thing removed, and add the rest back

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*.width + 2*height // review this line
   }

I formatted yor code and I saw that you didn’t close constructor. Also you have syntax error in calcPerimeter method.

1 Like