18, Make your own Method


#1


I did do everything okay..I think what do you think? Cause it fails me.

There was a problem with your syntax.

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.newWidth(8);
 rectangle.newHeight(6);

 }

#2

Hello @devninja93146,

What is the function of the closing curly brace at the end of your code. I believe that may be throwing you the error. Also, in your rectangle.setWidth statement, Width is capital in this.Width. Try making that lowercase, as JavaScript is case sensitive.

Try to fix these things, and reply if they don't work.


#3

It looks like you have an error in your code. Here's the message: TypeError: rectangle.newWidth is not a function

~~~~~

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.newWidth(8);
rectangle.newHeight(6);

~~~~~


#4

Jsut got this error message. I did as you said.


#5

Hey @devninja93146,

Your method name is rectangle.setHeight and rectangle.setWidth. Fix up your function calls and your code should work fine!


#6

Hi there,

I believe the issue is you've used '.newWidth ' .

Here is my working code :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;
};
// 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);

Hope this helps !


#7

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