Skip to content

Commit 8140ee0

Browse files
committed
#27 - Javascript
1 parent 8ac5a53 commit 8140ee0

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Roadmap/27 - SOLID OCP/javascript/parababire.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ console.log(`Hola mi nombre es ${engineer.name} soy ${engineer.getOccupation()}`
5252

5353
// Extra
5454

55+
/* Clase abstracta. No puede ser instanciada directamente pero es capaz de ser heredada por la descendencia */
56+
5557
class Operation {
5658
constructor() {
5759
if (new.target === Operation) {
@@ -107,10 +109,22 @@ class Divide extends Operation{
107109
}
108110
}
109111

112+
class Power extends Operation{
113+
constructor(a, b) {
114+
super()
115+
this.a = a
116+
this.b = b
117+
}
118+
execute() {
119+
return this.a ** this.b
120+
}
121+
}
122+
110123
class Calculator {
111124
constructor() {
112125
this.operations = {}
113126
}
127+
114128
addOperation(name, operation) {
115129
this.operations[name] = operation
116130
}
@@ -127,7 +141,11 @@ const calculadora = new Calculator()
127141
calculadora.addOperation('addition', new Addition(10, 2))
128142
calculadora.addOperation('subtraction', new Subtract(5, 2))
129143
calculadora.addOperation('multiplication', new Multiply(9, 2))
144+
calculadora.addOperation('division', new Divide(9, 2))
145+
calculadora.addOperation('power', new Power(5, 4))
130146

131147
console.log(calculadora.calculate('subtraction'))
132148
console.log(calculadora.calculate('addition'))
133149
console.log(calculadora.calculate('multiplication'))
150+
console.log(calculadora.calculate('division'))
151+
console.log(calculadora.calculate('power'))

0 commit comments

Comments
 (0)