Make Your Own Method -- Introduction to Objects


#1


https://www.codecademy.com/en/courses/spencer-sandbox/2/6?curriculum_id=506324b3a7dffd00020bf661


Oops, try again. It looks like you have an error in your code. Here's the message: ReferenceError: newHeight is not defined.

Help pls. Appreciate it :slight_smile:


var rectangle = new Object();
rectangle.height = 3;
rectangle.width = 4;
// here is our method to set the height
rectangle.setHeight = function (newHeight) {
  this.height = newHeight;
};
rectangle.setHeight = newHeight;

// help by finishing this method
rectangle.setHeight = function (newHeight) {
  this.height = newHeight;
};
rectangle.setWidth = newWidth;

// here change the width to 8 and height to 6 using our new methods
rectangle.setHeight(6);
rectangle.setWidth(8);


#2

rectangle.setHeight = newHeight;  // ReferenceError: newHeight is not defined.

First of all there is no value of newHeight outside of function.second why are you using this?

Instructions:
Finish the method setWidth. It should take a parameter newWidth. It will change the property width to the given parameter.

so you should do like

rectangle.setWidth = function(newWidth){  //  (newWidth) is a patameter  
this.width=newWidth;
};

Then use the two methods setHeight and setWidth to change rectangle's height to 6 and width to 8.

rectangle.setHeight(6); 
rectangle.setWidth(8);

#3

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