1+ //#18 - CONJUNTOS
2+ /*
3+ * EJERCICIO:
4+ * Utilizando tu lenguaje crea un conjunto de datos y realiza las siguientes
5+ * operaciones (debes utilizar una estructura que las soporte):
6+ * - Añade un elemento al final.
7+ * - Añade un elemento al principio.
8+ * - Añade varios elementos en bloque al final.
9+ * - Añade varios elementos en bloque en una posición concreta.
10+ * - Elimina un elemento en una posición concreta.
11+ * - Actualiza el valor de un elemento en una posición concreta.
12+ * - Comprueba si un elemento está en un conjunto.
13+ * - Elimina todo el contenido del conjunto.
14+ *
15+ * DIFICULTAD EXTRA (opcional):
16+ * Muestra ejemplos de las siguientes operaciones con conjuntos:
17+ * - Unión.
18+ * - Intersección.
19+ * - Diferencia.
20+ * - Diferencia simétrica.
21+ */
22+
23+ let log = console . log ;
24+
25+ window . addEventListener ( 'load' , ( ) => {
26+ const body = document . querySelector ( 'body' ) ;
27+ const title = document . createElement ( 'h1' ) ;
28+
29+ body . style . setProperty ( 'background' , '#000' ) ;
30+ body . style . setProperty ( 'text-align' , 'center' ) ;
31+
32+ title . textContent = 'Retosparaprogramadores #18.' ;
33+ title . style . setProperty ( 'font-size' , '3.5vmax' ) ;
34+ title . style . setProperty ( 'color' , '#fff' ) ;
35+ title . style . setProperty ( 'line-height' , '100vh' ) ;
36+
37+ body . appendChild ( title ) ;
38+
39+ setTimeout ( ( ) => {
40+ alert ( 'Retosparaprogramadores #18. Please open the Browser Developer Tools.' ) ;
41+ } , 2000 ) ;
42+ log ( 'Retosparaprogramadores #18' ) ;
43+ } ) ;
44+
45+
46+ let arr = [ 1 , 2 , 3 , 4 , 5 ] ;
47+ log ( arr ) // [1,2,3,4,5]
48+ // Adds an element to the end.
49+ arr . push ( 6 ) ;
50+ log ( arr ) ; // [1,2,3,4,5,6]
51+
52+ // Adds an element to the beginning.
53+ arr . unshift ( 0 ) ;
54+ log ( arr ) ; // [0, 1, 2, 3, 4, 5, 6]
55+ // Adds multiple elements in bulk to the end.
56+ arr2 = [ 7 , 8 , 9 , 10 ] ;
57+ arr . push ( ...arr2 ) ;
58+ log ( arr ) ; // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
59+
60+ // Adds multiple elements in bulk at a specific position.
61+ arr3 = [ 3.1 , 3.2 , 3.4 ]
62+ arr . splice ( 4 , 0 , ...arr3 ) ;
63+ log ( arr ) ; // [0, 1, 2, 3, 3.1, 3.2, 3.4, 4, 5, 6, 7, 8, 9, 10]
64+
65+ // Removes an element at a specific position.
66+ arr . splice ( 6 , 1 ) ;
67+ log ( arr ) ; // [0, 1, 2, 3, 3.1, 3.2, 4, 5, 6, 7, 8, 9, 10]
68+
69+ // Updates the value of an element at a specific position.
70+ arr . splice ( arr . length - 1 , 1 , 9.1 ) ;
71+ log ( arr ) ; // [0, 1, 2, 3, 3.1, 3.2, 4, 5, 6, 7, 8, 9, 9.1]
72+
73+ // Checks if an element is in a set.
74+ let nums = new Set ( ) ;
75+ arr . forEach ( n => nums . add ( n ) ) ;
76+ log ( 'Is 10 in nums: ' , nums . has ( 10 ) ) // false
77+
78+ // Removes all content from the set.
79+ nums . clear ( ) ;
80+ log ( nums ) ; // {size: 0}
81+
82+ // EXTRA DIFFICULTY (optional):
83+
84+ // Union.
85+ let arr4 = [ 10 , 11 , 12 , 13 , 14 ] ;
86+ let union = new Set ( [ ...arr , ...arr4 ] ) ;
87+ log ( 'union: ' , union ) ; // {0, 1, 2, 3, 3.1, …}
88+
89+ // Intersection.
90+ let arr5 = [ 1 , 4 , 5 , 6 , 15 , 16 , 17 , 18 , 19 ] ;
91+ let intersection = new Set ( ) ;
92+ arr5 . forEach ( n => {
93+ if ( union . has ( n ) ) {
94+ intersection . add ( n ) ;
95+ } } ) ;
96+
97+ log ( 'Intersection:' , [ ...intersection ] ) ; // [1, 4, 5, 6]
98+
99+ // Difference.
100+ let difference = new Set ( arr ) ;
101+ arr5 . forEach ( n => {
102+ if ( difference . has ( n ) ) {
103+ difference . delete ( n ) ;
104+ } } ) ;
105+
106+ log ( 'Difference:' , [ ...difference ] ) ; // [0, 2, 3, 3.1, 3.2, 7, 8, 9, 9.1]
107+
108+ // Symmetric difference.
109+ let symmetricDiff = new Set ( [ ...arr , ...arr5 ] ) ;
110+ arr5 . forEach ( n => {
111+ if ( union . has ( n ) ) {
112+ symmetricDiff . delete ( n ) ;
113+ }
114+ } ) ;
115+ arr . forEach ( n => {
116+ if ( arr5 . includes ( n ) ) {
117+ symmetricDiff . delete ( n ) ;
118+ } } ) ;
119+
120+ log ( 'Symmetric Difference:' , [ ...symmetricDiff ] ) ; // [0, 2, 3, 3.1, 3.2, 7, 8, 9, 9.1, 15, 16, 17, 18, 19]
0 commit comments