# OOJS 1 - Practice

**URL:** <https://discourse.mozilla.org/t/oojs-1-practice/58707>\
**Category:** Learn\
**Tags:** participation, learning\
**Created:** [April 26, 2020, 3:58am UTC](https://discourse.mozilla.org/t/oojs-1-practice/58707 "2020-04-26T03:58:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ranjanrnj44](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/ranjanrnj44/32/32875_2.png) [@ranjanrnj44](https://discourse.mozilla.org/u/ranjanrnj44)\
**Post date:** [April 26, 2020, 3:58am UTC](https://discourse.mozilla.org/t/oojs-1-practice/58707/1 "2020-04-26T03:58:04Z")

</div>

**OOJS 1 solution1:**

function Shape(name, sides, sideLength) {  
this.name = name;  
this.sides = sides;  
this.sideLength = sideLength;  
this.calcPerimeter = function() {  
return (`Result of ${this.name} is : ${this.sides*this.sideLength}`);  
};  
}

// create object  
let square = new Shape(“square”, 4, 5);  
console.log(square.calcPerimeter());

let triangle = new Shape(“triangle”, 3, 3);  
console.log(triangle.calcPerimeter());

**OOJS 1 solution2:**

```
function Shape(name, sides, sideLength) {
    this.name = name;
    this.sides = sides;
    this.sideLength = sideLength;
    this.calcPerimeter = function() {
        if (this.name === "square") {
            console.log(sideLength * sides);
        } else if (this.name === "triangle") {
            console.log(sideLength * sides);
        }
    };
}

```

// create object  
let square = new Shape(“square”, 4, 5);  
console.log(square.calcPerimeter());

let triangle = new Shape(“triangle”, 3, 3);  
console.log(triangle.calcPerimeter());

---

<div class="post-metadata">

**Author:** ![chrisdavidmills](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/chrisdavidmills/32/21688_2.png) [@chrisdavidmills](https://discourse.mozilla.org/u/chrisdavidmills)\
**Post date:** [April 27, 2020, 9:08am UTC](https://discourse.mozilla.org/t/oojs-1-practice/58707/2 "2020-04-27T09:08:13Z")

</div>

@ranjanrnj44 This looks good as well.

---

<div class="post-metadata">

**Author:** ![Mary\_Usaji](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@Mary\_Usaji](https://discourse.mozilla.org/u/Mary_Usaji)\
**Post date:** [September 8, 2023, 2:07pm UTC](https://discourse.mozilla.org/t/oojs-1-practice/58707/3 "2023-09-08T14:07:56Z")

</div>

I thought the calculation for perimeter is

Perimeter = (L+W)\*2

---

<div class="post-metadata">

**Author:** ![mikoMK](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/mikomk/32/50456_2.png) [@mikoMK](https://discourse.mozilla.org/u/mikoMK)\
**Post date:** [September 11, 2023, 11:43am UTC](https://discourse.mozilla.org/t/oojs-1-practice/58707/4 "2023-09-11T11:43:21Z")

</div>

Since the exercise is about shapes of equal length, `sideLength * sides` is fine. (The constructor has only one parameter for side length.)  
You’re formula would be needed if we’re talking about rectangles where the width and the height are different.  
The most general form would be arbitrary shapes where we have a different length (and therefore parameter) for every side.
