Skip to content

Commit 5ce1ffb

Browse files
committed
#27 - JavaScript
1 parent 6e279ee commit 5ce1ffb

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// Wrong way
2+
class ShapeCalculator {
3+
calculateArea(shape) {
4+
if (shape.type === 'circle'){
5+
return Math.PI * Math.pow(shape.radius, 2);
6+
} else if (shape.type === 'square') {
7+
return Math.pow(shape.side, 2);
8+
} else {
9+
throw new Error('Shape not supported');
10+
}
11+
}
12+
}
13+
14+
const calculator = new ShapeCalculator();
15+
console.log(calculator.calculateArea({ type: 'circle', radius: 5 }));
16+
console.log(calculator.calculateArea({ type: 'square', side: 4 }));
17+
18+
// Correct way
19+
class Shape {
20+
calculateArea(shape) {
21+
throw new Error('calculateArea() must be implemented');
22+
}
23+
}
24+
25+
class Circle extends Shape {
26+
constructor(radius) {
27+
super();
28+
this.radius = radius;
29+
}
30+
31+
calculateArea() {
32+
return Math.PI * Math.pow(this.radius, 2);
33+
}
34+
}
35+
36+
class Square extends Shape {
37+
constructor(side) {
38+
super();
39+
this.side = side;
40+
}
41+
42+
calculateArea() {
43+
return Math.pow(this.side, 2);
44+
}
45+
}
46+
47+
function printArea(shape) {
48+
console.log(`Area: ${shape.calculateArea()}`);
49+
}
50+
51+
const circle = new Circle(5);
52+
const square = new Square(4);
53+
printArea(circle);
54+
printArea(square);
55+
56+
// Extra Exercise //
57+
class Operation {
58+
calculate(a, b) {
59+
throw new Error('calculateOperation() must be implemented');
60+
}
61+
}
62+
63+
class Addition extends Operation {
64+
calculate(a, b) {
65+
return a + b;
66+
}
67+
}
68+
69+
class Subtraction extends Operation {
70+
calculate(a, b) {
71+
return a - b;
72+
}
73+
}
74+
75+
class Multiplication extends Operation {
76+
calculate(a, b) {
77+
return a * b;
78+
}
79+
}
80+
81+
class Division extends Operation {
82+
calculate(a, b) {
83+
if (b === 0) throw new Error('Division by zero is not allowed');
84+
return a / b;
85+
}
86+
}
87+
88+
class Pow extends Operation {
89+
calculate(a, b) {
90+
return Math.pow(a, b);
91+
}
92+
}
93+
94+
class Calculator {
95+
constructor() {
96+
this.operations = {};
97+
}
98+
99+
addOperation(name, operation) {
100+
this.operations[name] = operation;
101+
}
102+
103+
calculate(name, a, b) {
104+
const operation = this.operations[name];
105+
if (!operation) throw new Error(`Operation "${name}" not supported`);
106+
return operation.calculate(a, b);
107+
}
108+
}
109+
110+
const calculator2 = new Calculator();
111+
calculator2.addOperation('addition', new Addition());
112+
calculator2.addOperation('subtraction', new Subtraction());
113+
calculator2.addOperation('multiplication', new Multiplication());
114+
calculator2.addOperation('division', new Division());
115+
calculator2.addOperation('pow', new Pow());
116+
117+
console.log(calculator2.calculate('addition', 2, 10));
118+
console.log(calculator2.calculate('subtraction', 2, 10));
119+
console.log(calculator2.calculate('multiplication', 2, 10));
120+
console.log(calculator2.calculate('division', 10, 5));
121+
console.log(calculator2.calculate('pow', 10, 2));
122+
123+

0 commit comments

Comments
 (0)