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 trialrichi richi
552 PointsPractice Getters and Setters in JavaScript
Create an array called areas with three elements so that the first element (areas[0]) holds the value of the area property of rect1, the second element holds the value of the area property of rect2, and the third element holds the value of the area property of rect3
Hello, the question ask for an array that hold 3 values... I created the array but is showing me an error . What can be wrong?
Thanks
class Rectangle {
constructor(width, length){
this.width = width;
this.length = length;
}
get area(){
return this.length * this.width;
}
}
const rect1 = new Rectangle(10, 5);
const rect2 = new Rectangle(6, 12);
const rect3 = new Rectangle(15, 20);
var areas = [
rect1,
rect2,
rect3
];
4 Answers
Eric M
11,546 PointsHi richi,
Your array has the three rect objects in it. The task asks for the array to instead contain the areas of each rect object.
Cheers,
Eric
Khalid Ahmed
Front End Web Development Techdegree Graduate 15,430 PointsIn the arry access the 'area' method within each objects!
Chardai Anderson
10,328 Pointsclass Rectangle { constructor(width, length){ this.width = width; this.length = length; }
get area() { return this.width * this.length; }
}
const areas = [50,72,300];
const rect1 = new Rectangle(10, 5); const rect2 = new Rectangle(6, 12); const rect3 = new Rectangle(15, 20);
Be sure to enter value of the rects. Reference^
Gremyko Coleman
9,756 PointsI know this is old but this is how I figured it out
you set the length and width values of rect1, rect2, rect3 by creating a new Rectangle object
then with "const areas" you call each object value rect1 , rect2, rect3 and add the get area method to it and it computes the area.
output is " areas [50, 72 , 300]"
class Rectangle {
constructor(width, length){
this.width = width;
this.length = length;
}
get area(){
return this.width * this.length;
}
}
const rect1 = new Rectangle(10, 5);
const rect2 = new Rectangle(6, 12);
const rect3 = new Rectangle(15, 20);
const areas = [rect1.area, rect2.area, rect3.area];