Skip to content

Commit c5a86a7

Browse files
authored
Merge pull request mouredev#6107 from parababire/main
#27 - Javascript
2 parents 0d9b8be + 8140ee0 commit c5a86a7

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
// Ejercicio
2+
3+
/* Incorrecto */
4+
5+
class Animal {
6+
constructor(sound) {
7+
this.sound = sound
8+
}
9+
10+
makeSound () {
11+
switch (this.sound) {
12+
case 'meau':
13+
console.log(`The animal makes meau!!!`);
14+
break;
15+
case 'woof':
16+
console.log(`The animal makes woof!!!`);
17+
break;
18+
default:
19+
console.log('Please incert an animal sound!');
20+
break;
21+
}
22+
}
23+
}
24+
const animal = new Animal('meau')
25+
animal.makeSound()
26+
27+
/* Correcto */
28+
29+
class Person {
30+
constructor(name, occupation) {
31+
this.name = name
32+
this.occupation = occupation
33+
}
34+
35+
getOccupation() {
36+
return this.occupation.getOccupation()
37+
}
38+
}
39+
40+
class PersonOccupation {
41+
getOccupation() {}
42+
}
43+
44+
class IndustrialEngineer extends PersonOccupation {
45+
getOccupation() {
46+
return 'I am an industrial engineer.'
47+
}
48+
}
49+
50+
const engineer = new Person('Yojan', new IndustrialEngineer())
51+
console.log(`Hola mi nombre es ${engineer.name} soy ${engineer.getOccupation()}`)
52+
53+
// Extra
54+
55+
/* Clase abstracta. No puede ser instanciada directamente pero es capaz de ser heredada por la descendencia */
56+
57+
class Operation {
58+
constructor() {
59+
if (new.target === Operation) {
60+
throw new TypeError("No puedes instanciar la clase abstracta 'Operation' directamente")
61+
}
62+
}
63+
execute() {
64+
throw new Error("Método 'execute()' debe ser implementado en la clase hija")
65+
}
66+
}
67+
68+
class Addition extends Operation{
69+
constructor(a, b) {
70+
super()
71+
this.a = a
72+
this.b = b
73+
}
74+
execute() {
75+
return this.a + this.b
76+
}
77+
}
78+
79+
class Subtract extends Operation{
80+
constructor(a, b) {
81+
super()
82+
this.a = a
83+
this.b = b
84+
}
85+
execute() {
86+
return this.a - this.b
87+
}
88+
}
89+
90+
class Multiply extends Operation{
91+
constructor(a, b) {
92+
super()
93+
this.a = a
94+
this.b = b
95+
}
96+
execute() {
97+
return this.a * this.b
98+
}
99+
}
100+
101+
class Divide extends Operation{
102+
constructor(a, b) {
103+
super()
104+
this.a = a
105+
this.b = b
106+
}
107+
execute() {
108+
return this.a / this.b
109+
}
110+
}
111+
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+
123+
class Calculator {
124+
constructor() {
125+
this.operations = {}
126+
}
127+
128+
addOperation(name, operation) {
129+
this.operations[name] = operation
130+
}
131+
132+
calculate(name) {
133+
if (!this.operations[name]) {
134+
throw new Error(`Operación ${name} no es valida`)
135+
}
136+
return this.operations[name].execute()
137+
}
138+
}
139+
140+
const calculadora = new Calculator()
141+
calculadora.addOperation('addition', new Addition(10, 2))
142+
calculadora.addOperation('subtraction', new Subtract(5, 2))
143+
calculadora.addOperation('multiplication', new Multiply(9, 2))
144+
calculadora.addOperation('division', new Divide(9, 2))
145+
calculadora.addOperation('power', new Power(5, 4))
146+
147+
console.log(calculadora.calculate('subtraction'))
148+
console.log(calculadora.calculate('addition'))
149+
console.log(calculadora.calculate('multiplication'))
150+
console.log(calculadora.calculate('division'))
151+
console.log(calculadora.calculate('power'))

0 commit comments

Comments
 (0)