OOJS 3 - example

This morning I was working on object, example problem OOJS-3. Hope I did, help me to improve on it sir @chrisdavidmills

 class Shape {

    constructor(name, sides, sideLength) {
        //below 3 are properties
        this.name = name;
        this.sides = sides;
        this.sideLength = sideLength;

        //method
        this.calcPerimeter = function() {
            let peri = this.sides * this.sideLength;
            console.log(peri);
        }
    };
}

//create a class(square - child)that extends from (Shape - parent)
class Square extends Shape {
    constructor(sideLength) {
            super("square", 4, sideLength);
        }

        //create a method calcArea() & calcPerimeter()
    calcArea() {
        return (this.sideLength * this.sideLength);
    }

    calcPerimeter() {
        return (this.sides * this.sideLength);
    }

}

//create a class(square - child)that extends from (Shape - parent)
class Triangle extends Shape {
    constructor(sideLength) {
            super("Triangle", 3, sideLength);
        }

        //create method calcArea() & calcPerimeter()
    calcArea() {
        return (this.sideLength * this.sideLength);
    }
    calcPerimeter() {
        return (this.sides * this.sideLength);
    }

}

//pass only sides - SQUARE

let square = new Square(5);
console.log(square.name);
console.log(square.sides);
console.log(square.calcArea());
console.log(square.calcPerimeter());



//pass only sides - TRIANGLE

let triangle = new Triangle(3);
console.log(triangle.name);
console.log(triangle.sides);
console.log(triangle.calcArea());
console.log(triangle.calcPerimeter());

Well hi there @ranjanrnj44! I’ve had a look over your code and it looks like it works pretty much fine. You can see what we did here

The only thing I’d say is that the area of an equalateral triangle is not just sideLength * sideLength. You’d have to plug in the correct formula here.

1 Like

:grimacing: I missed the formulae. And I really thank you Sir, I’ve been following your comments that you’ve made and tips you give to all friends here, It’s really helpful & improves my coding skills.