We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

# Day 4: Create a Rectangle Object

# Day 4: Create a Rectangle Object

aemulor + 0 comments My answer:

function Rectangle(a, b) { this.length = a; this.width = b; this.area = a * b; this.perimeter = 2 * (a + b); }

or with ES6 (but without usage of defined code in problem):

class Rectangle { constructor(a, b) { this.length = a; this.width = b; this.area = a * b; this.perimeter = 2 * (a + b); } }

hans_sagita + 0 comments function Rectangle(a, b) { return { length: a, width: b, perimeter: 2 * (a + b), area: a*b } }

CodeTrafficker + 0 comments Similar to hans-sagita, I did:

function Rectangle(a, b) { return { length: a, width: b, perimeter: (2*a) + (2*b), area: (a*b) } }

For some reason when I did this.length = a; etc, it did not work. Above it returns a set of keys and values within brackets, ie, an object.

Also, this has been the fastest challenge to pass in the 10 Days of JS challenge for me, like <5 to 10 minutes (?).

_clfm_ + 0 comments ## Clean & correct

Let me know if you have any questions!

function Rectangle(a, b) { this.length = a; this.width = b; this.perimeter = 2 * (a + b); this.area = a * b; }

(From my HackerRank repo on GitHub.)

kirilzh + 0 comments Here's my submission:

function Rectangle(a, b) { return { length: a, width: b, perimeter: 2 * ( a + b ), area: a * b } }

Load more conversations

Sort 54 Discussions, By:

Please Login in order to post a comment