Skip to content

Commit 814a4b2

Browse files
authored
Merge pull request mouredev#6757 from caterinarodriguezdev/08---javascript
#8 - javascript
2 parents 21fa406 + 7f7e693 commit 814a4b2

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* EJERCICIO:
3+
* Explora el concepto de clase y crea un ejemplo que implemente un inicializador,
4+
* atributos y una función que los imprima (teniendo en cuenta las posibilidades
5+
* de tu lenguaje).
6+
* Una vez implementada, créala, establece sus parámetros, modifícalos e imprímelos
7+
* utilizando su función.
8+
*
9+
* DIFICULTAD EXTRA (opcional):
10+
* Implementa dos clases que representen las estructuras de Pila y Cola (estudiadas
11+
* en el ejercicio número 7 de la ruta de estudio)
12+
* - Deben poder inicializarse y disponer de operaciones para añadir, eliminar,
13+
* retornar el número de elementos e imprimir todo su contenido.
14+
*/
15+
16+
class Robot {
17+
18+
constructor (nombre, rapidez, superPoder) {
19+
this.nombre = nombre ?? 'mimi';
20+
this.rapidez = rapidez ?? '0';
21+
this.superPoder = superPoder ?? 'iluminidad'
22+
}
23+
24+
print () {
25+
console.log(`>>>>>>>>>>> Mi robot se llama ${this.nombre} con una velocidad de ${this.rapidez} y su máximo poder es ${this.superPoder} <<<<<<<<<<<<<<<`);
26+
}
27+
}
28+
29+
const miRobot = new Robot();
30+
miRobot.print();
31+
32+
class Stack {
33+
34+
constructor (arr) {
35+
this.stack = arr;
36+
}
37+
38+
push (item) {
39+
this.stack.push(item);
40+
}
41+
42+
pop () {
43+
this.stack.pop();
44+
}
45+
46+
peek () {
47+
return this.stack[this.stack.length - 1];
48+
}
49+
50+
size () {
51+
return this.stack.length;
52+
}
53+
54+
print () {
55+
console.log(this.stack);
56+
}
57+
}
58+
59+
class Queue {
60+
61+
constructor (arr) {
62+
this.queue = arr;
63+
}
64+
65+
enqueue (item) {
66+
this.queue.push(item);
67+
}
68+
69+
dequeue () {
70+
if (this.isEmpty()) {
71+
return null;
72+
} else {
73+
return this.queue.shift();
74+
}
75+
}
76+
77+
isEmpty() {
78+
return this.queue.length === 0;
79+
}
80+
81+
size () {
82+
return this.queue.length;
83+
}
84+
85+
front () {
86+
if (this.isEmpty()) {
87+
return null;
88+
} else {
89+
return this.queue[0];
90+
}
91+
}
92+
93+
print () {
94+
console.log(this.queue);
95+
}
96+
}
97+
98+
const cola = new Queue([11, 22, 33]);
99+
cola.print();
100+
101+
const pila = new Stack([10, 20, 30]);
102+
pila.print();

0 commit comments

Comments
 (0)