Skip to content

Commit 47097df

Browse files
authored
Merge pull request mouredev#2897 from nlarrea/js-reto08
#8 - javascript
2 parents 19fc283 + d778744 commit 47097df

File tree

1 file changed

+166
-0
lines changed

1 file changed

+166
-0
lines changed
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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+
17+
class Person {
18+
19+
languages = []
20+
21+
constructor(name, username, age) {
22+
this.name = name;
23+
this.username = username;
24+
this.age = age;
25+
}
26+
27+
printPerson() {
28+
console.log(`\nPrinting ${this.username}'s data:`);
29+
console.log('name:', this.name);
30+
console.log('age:', this.age);
31+
32+
if (this.languages) {
33+
console.log('This person uses the following languages to code:');
34+
for (let language of this.languages) {
35+
console.log(` - ${language}`);
36+
}
37+
}
38+
}
39+
}
40+
41+
42+
me = new Person(name='Naia', username='nlarrea', age=25);
43+
me.printPerson();
44+
me.languages = ['Python', 'JavaScript', 'Kotlin'];
45+
me.printPerson();
46+
me.age = 26;
47+
me.printPerson();
48+
49+
50+
/*
51+
* DIFICULTAD EXTRA (opcional):
52+
* Implementa dos clases que representen las estructuras de Pila y Cola (estudiadas
53+
* en el ejercicio número 7 de la ruta de estudio)
54+
* - Deben poder inicializarse y disponer de operaciones para añadir, eliminar,
55+
* retornar el número de elementos e imprimir todo su contenido.
56+
*/
57+
58+
59+
console.log('\nLIFO:\n')
60+
61+
62+
/**
63+
* Stack is a LIFO (Last In First Out).
64+
*/
65+
class Stack{
66+
constructor() {
67+
this.stack = [];
68+
}
69+
70+
add(...items) {
71+
this.stack = this.stack.concat([...items]);
72+
}
73+
74+
remove() {
75+
if (this.length() === 0) {
76+
return null;
77+
}
78+
return this.stack.pop();
79+
}
80+
81+
length() {
82+
return this.stack.length;
83+
}
84+
85+
print() {
86+
for (let item of this.stack.slice().reverse()) {
87+
console.log(item);
88+
}
89+
}
90+
}
91+
92+
93+
lifo = new Stack();
94+
lifo.add(1, 2, 3);
95+
lifo.print();
96+
/*
97+
3
98+
2
99+
1
100+
*/
101+
console.log('LIFO length:', lifo.length());
102+
// LIFO length: 3
103+
104+
lifo.remove(); // This would print the number 3 because it's the removed one
105+
lifo.print();
106+
/*
107+
2
108+
1
109+
*/
110+
console.log('LIFO length:', lifo.length());
111+
// LIFO length: 2
112+
113+
114+
console.log('\nFIFO:\n');
115+
116+
117+
/**
118+
* A Queue is a FIFO (First In, First Out).
119+
*/
120+
class Queue {
121+
constructor() {
122+
this.queue = [];
123+
}
124+
125+
add(...items) {
126+
this.queue = this.queue.concat([...items]);
127+
}
128+
129+
remove() {
130+
if (this.length() === 0) {
131+
return null;
132+
}
133+
return this.queue.shift();
134+
}
135+
136+
length() {
137+
return this.queue.length;
138+
}
139+
140+
print() {
141+
for (let item of this.queue) {
142+
console.log(item);
143+
}
144+
}
145+
}
146+
147+
148+
fifo = new Queue();
149+
fifo.add(1, 2, 3);
150+
fifo.print();
151+
/*
152+
1
153+
2
154+
3
155+
*/
156+
console.log('FIFO length:', fifo.length());
157+
// FIFO length: 3
158+
159+
fifo.remove();
160+
fifo.print();
161+
/*
162+
2
163+
3
164+
*/
165+
console.log('FIFO length:', fifo.length());
166+
// FIFO length: 2

0 commit comments

Comments
 (0)