Constructors With Methods


#1

SyntaxError: missing ) after argument list

Replace this line with your code. 

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 + this.width)
  };
      }


var rex = new Rectangle(7,9);
var perimeter = rex.calcPerimeter();//this is done to get value of perimeter
var area = rex.calcArea();

console.log("rex is a rectangle with"+rex.calcPerimeter +" as perimeter and" rex.calcArea+ "as area")

Where did my console.log go wrong???


#2

In your console.log you forgot that you were using two methods and did not include () for rex.calcArea rectify that and your code should be fine. :grin:

Additional Note: Since you have stored these two methods in variables,

You may also just console.log it like,

console.log("rex is a rectangle with "+perimeter +" as perimeter and" area + "as area")


#3

I don't why, but you need to get rid of

this line and it should work...

The reason has to do with you not using parenthesis and arguments when calling rex.calcPerimeter and rex.calcArea.

EDIT: or rectify it, you don't necessarily have to get rid of it.


#4

Actually that was the first thing that i did, but it didn't work.
Then i tried it without parenthesis and it didn't still work.

Just Included parenthesis now and it isn't working.


#5

Actually that was what i did first . It isn't working.


#6

How could you have done both my solution and @kyleaw first :sweat_smile:

Your console .log here,

console.log("rex is a rectangle with"+rex.calcPerimeter +" as perimeter and" rex.calcArea+ "as area")

Should either have parentheses after the methods or refer to your two variables containing teh two methods.


#7

You mean this?
console.log("rex is a rectangle with"+rex.calcPerimeter() +" as perimeter and" rex.calcArea()+ "as area")


#8

Just found your error after putting your code under a 600x microscope (my eyes) :laughing:

console.log("rex is a rectangle with"+rex.calcPerimeter() +" as perimeter and " rex.calcArea()+ "as area") // + sign missing between " as perimeter and " rex.calcArea() in your console.log. :smile:

just add the +,

console.log("Rex is a rectangle with " + perimeter + " as perimeter and" + area + "as area.")


#9

Lol....Clumsy me. Thanks :slight_smile:

I admire your microscope.


#10

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