Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Marcelo Retana
2,534 PointsI need to calculate a perimeter with pure JS
I need to define a new method on line 8, calcPerimeter, which calculates and returns the perimeter for a Rectangle in terms of height and width.
The hint that was given to me is:
Use calcArea as a guide to code up calcPerimeter. Remember that a rectangles perimeter is all four sides added together, or 2 * height + 2 * width.
When referring to a rectangle's properties, be sure to use this.propertyName:
I tried in some different ways but I couldn't get it***
And here the code:
function Rectangle(height, width) {
this.height = height;
this.width = width;
this.calcArea = function() {
return this.height * this.width;
};
// put our perimeter function here!
function calcPerimeter() {
return 2 * this.height + 2 * this.width;
}
}
var rex = new Rectangle(7,3);
var area = rex.calcArea();
var perimeter = rex.calcPerimeter();
1 Answer
Dino Paškvan
Courses Plus Student 44,108 PointsMake sure that it's a method:
this.calcPerimiter = function() {
// return statement
}
Marcelo Retana
2,534 PointsMarcelo Retana
2,534 PointsYisus, it works already, THANK YOU!!!
Marcelo Retana
2,534 PointsMarcelo Retana
2,534 PointsYisus, it works already, THANK YOU!!!