Solution please


#1

https://www.codecademy.com/en/courses/spencer-sandbox/2/6

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

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

var rectangle = new Object();
rectangle.Height = 3;
rectangle.width = 4;

// here is our method to set the length
rectangle.setLength = function (newLength) {
 this.Height = newHeight;
 };

// help by finishing this method
rectangle.setWidth = function(newWidth){
this.width = newWidth;
};
// here change the width to 8 and length to 6 using our new methods
//rectangle.setLength = setLength;
//rectangle.setWidth = setWidth;
rectangle.setHeight(6); 
rectangle.setWidth(8);

i cant fix how .. ? can anyone explain exactly what should i add to the code?


#2

The variable should be height not Height:

rectangle.height = 3;

Check the instructions closely for the exact names of the methods given. Do not create your own names. Use the ones in the instructions.


#3

hello , i diid but browser told me this below comment ,

Oops, try again. It looks like you have an error in your code. Here's the message: TypeError: rectangle.setHeight is not a function

also this is the course request :slightly_smiling:

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

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


#4

The error is because you have named the method, setLength. Change the name to setHeight as instructed:

Then use the two methods setHeight and setWidth

Be sure to repair the variable name:

    rectangle.setHeight = function (newHeight) {
        this.height = newHeight;
    };

#5

var rectangle = new Object();
rectangle.height = 3;
rectangle.width = 4;

// here is our method to set the length
rectangle.setHeight = function (newHeight){
this.height = newHeight;
};

// help by finishing this method
rectangle.setWidth = function(newWidth){
this.width = newWidth;
};
// here change the width to 8 and length to 6 using our new methods
rectangle.setHeight = setHeight;
rectangle.setWidth = setWidth;
rectangle.setHeight(6);
rectangle.setWidth(8);

now as you see i done like above .. can you see any bug ??


#6

There are two possibilities...

  1. Write setHeight as a standalone function; or,
  2. Write setHeight as a method of the rectangle object.

The first approach might look something like this:

var setHeight = function (newHeight) {};
var setWidth = function (newWidth) {};

To make these methods of the object, we assign them to an attribute:

rectangle.setHeight = setHeight;
rectangle.setWidth = setWidth;

Now they are properties of the object.

The second is to write the methods directly on the object:

var rectangle = {
    height: 3,
    width: 4,
    setHeight: function (newHeight) {},
    setWidth: function (newWidth) {}
};

A third approach is basically what you have already, except we would not make the assignments at the end. setHeight and setWidth are not defined. Comment those two lines and try your code.


#7

thanks alot it helped me and i got success to the next exercises , regards