dz1-by-onfkik 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/app.js +19 -0
  2. package/figures.js +92 -0
  3. package/package.json +13 -0
package/app.js ADDED
@@ -0,0 +1,19 @@
1
+ const {
2
+ Square,
3
+ Rectangle,
4
+ Triangle
5
+ } = require("./figures");
6
+
7
+ const figures = [
8
+ new Square(5),
9
+ new Rectangle(6, 4),
10
+ new Triangle(3, 4, 5)
11
+ ];
12
+
13
+ figures.forEach(figure => {
14
+ console.log("----------------------");
15
+ console.log("Name:", figure.name);
16
+ figure.info();
17
+ console.log("Area:", figure.area());
18
+ console.log("Perimeter:", figure.perimeter());
19
+ });
package/figures.js ADDED
@@ -0,0 +1,92 @@
1
+ class Figure {
2
+ constructor(name) {
3
+ this._name = name;
4
+ }
5
+
6
+ get name() {
7
+ return this._name;
8
+ }
9
+
10
+ info() {
11
+ console.log(`Figure: ${this.name}`);
12
+ }
13
+
14
+ area() {
15
+ return 0;
16
+ }
17
+
18
+ perimeter() {
19
+ return 0;
20
+ }
21
+ }
22
+
23
+ class Square extends Figure {
24
+ constructor(side) {
25
+ super("Square");
26
+ this.side = side;
27
+ }
28
+
29
+ info() {
30
+ console.log(`Square`);
31
+ console.log(`Sides: ${this.side}, ${this.side}, ${this.side}, ${this.side}`);
32
+ }
33
+
34
+ area() {
35
+ return this.side ** 2;
36
+ }
37
+
38
+ perimeter() {
39
+ return this.side * 4;
40
+ }
41
+ }
42
+
43
+ class Rectangle extends Figure {
44
+ constructor(width, height) {
45
+ super("Rectangle");
46
+ this.width = width;
47
+ this.height = height;
48
+ }
49
+
50
+ info() {
51
+ console.log(`Rectangle`);
52
+ console.log(`Sides: ${this.width}, ${this.height}, ${this.width}, ${this.height}`);
53
+ }
54
+
55
+ area() {
56
+ return this.width * this.height;
57
+ }
58
+
59
+ perimeter() {
60
+ return 2 * (this.width + this.height);
61
+ }
62
+ }
63
+
64
+ class Triangle extends Figure {
65
+ constructor(a, b, c) {
66
+ super("Triangle");
67
+ this.a = a;
68
+ this.b = b;
69
+ this.c = c;
70
+ }
71
+
72
+ info() {
73
+ console.log(`Triangle`);
74
+ console.log(`Sides: ${this.a}, ${this.b}, ${this.c}`);
75
+ }
76
+
77
+ perimeter() {
78
+ return this.a + this.b + this.c;
79
+ }
80
+
81
+ area() {
82
+ const p = this.perimeter() / 2;
83
+ return Math.sqrt(p * (p - this.a) * (p - this.b) * (p - this.c));
84
+ }
85
+ }
86
+
87
+ module.exports = {
88
+ Figure,
89
+ Square,
90
+ Rectangle,
91
+ Triangle
92
+ };
package/package.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "dz1-by-onfkik",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "app.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "keywords": [],
10
+ "author": "",
11
+ "license": "ISC",
12
+ "type": "commonjs"
13
+ }