18. Make Your own method


#1

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;

// help by finishing this method
rectangle.setWidth = function (newWidth) {
this.width = newWidth;

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

};

};

This code turns up with error: "Oops, try again. Make sure to set rectangle's width to 8!" But it is clearly set to 8? What mistake am I making? I've been looking for errors for around 15 minutes now.


#2

@coursejumper79205,

With

console.log(rectangle);
console.log("=========");
console.log( rectangle.setHeight.toString());

you will see that
as you are forgetting the closing-curly-bracket-}
to close the setHeight Method
BEFORE
you define the setWidth Method.....
causing the setWidth function to be integrated in the setHeight Method

[the output]

{ height: 3, width: 4, setHeight: [Function] }
=========
function (newHeight) {
 this.height = newHeight;

// help by finishing this method
rectangle.setWidth = function (newWidth) {
this.width = newWidth;

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

};

}

#3

Thank you very much! I cant believe I didnt see that before!


#4

var rectangle = new Object();
rectangle.height = 3;
rectangle.width = 4;
// aqui esta nosso metodo para definir a altura
rectangle.setHeight = function (newHeight) {
this.height = newHeight;
};
// ajude finalizando este metodo
rectangle.setWidth = function(newWidth){
this.width = newWidth;
}
// aqui, mude a largura para 8 e a altura para 6 usando nossos novos metodos
rectangle.setWidth(8);
rectangle.setHeight(6);


#5

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;
};
// help by finishing this method
rectangle.setWidth = function (newWidth) {
this.width = newWidth;
};

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

Try That


#6

hi,

This is my code. and i kept getting the same error: "Oops, try again. Make sure to set rectangle's width to 8!" But it is clearly set to 8?"
can you tell mw where i missed? thanks.

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;
};
// help by finishing this method
rectangle.setWidth = function(newWidth) {
this.widht = newWidth;
};
// here change the width to 8 and height to 6 using our new methods
rectangle.setWidth(8);
rectangle.setHeight(6);


#7

@vulcaniser666,

Have a close look at

this.widht = newWidth;


#8

Right you are. thanks, man, you are very thorough.


#9

You misspelled through.You typed thorough.


#10

I am having a similar problem like the people above, it says
TypeError: rectangle.setHeight is not a function, even though it is there.

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;
};
// help by finishing this method
rectangle.setHeight = 6;
  

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

#11

@astronautraccon,
if you use

rectangle.setHeight = 6;

you are redefining the setHeight property
and therefor you do not have a setHeight() Method.....

As a proof

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;
};
// help by finishing this method
rectangle.setHeight = 6;
console.log( rectangle );

#12

It still doesn't work I get

Oops, try again.
Make sure to set rectangle's height to 6!

#13

@astronautraccon
Then remove

rectangle.setHeight = 6;

from the code you presented in your Original Post.


#14

Thanks for the help!