|
| 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 Developer { |
| 18 | + constructor(name, code, level, alias = 'Sin alias') { |
| 19 | + this.name = name |
| 20 | + this.code = code |
| 21 | + this.level = level |
| 22 | + this.alias = alias |
| 23 | + } |
| 24 | + |
| 25 | + infoDev = () => `${this.name} tiene un nivel de ${this.level}, programando en ${this.exp}` |
| 26 | + |
| 27 | + setAlias = function (alias) { |
| 28 | + this.alias = alias |
| 29 | + } |
| 30 | + |
| 31 | +} |
| 32 | + |
| 33 | +let developer1 = new Developer('Diego', 'JavaScript', 'principiante') |
| 34 | + |
| 35 | +console.log(developer1.infoDev()); |
| 36 | + |
| 37 | +console.log(developer1.alias); |
| 38 | + |
| 39 | +developer1.setAlias('DevsDav') |
| 40 | + |
| 41 | +console.log(developer1.alias) |
| 42 | + |
| 43 | + |
| 44 | +/* |
| 45 | +* |
| 46 | +* DIFICULTAD EXTRA (opcional): |
| 47 | +* Implementa dos clases que representen las estructuras de Pila y Cola (estudiadas |
| 48 | +* en el ejercicio número 7 de la ruta de estudio) |
| 49 | +* - Deben poder inicializarse y disponer de operaciones para añadir, eliminar, |
| 50 | +* retornar el número de elementos e imprimir todo su contenido. |
| 51 | +* |
| 52 | +*/ |
| 53 | + |
| 54 | +class Stack { |
| 55 | + |
| 56 | + constructor(initialElements = []) { |
| 57 | + this.stack = [...initialElements] |
| 58 | + this.lengthStack = this.stack.length |
| 59 | + } |
| 60 | + |
| 61 | + addElement(element) { |
| 62 | + this.stack.push(element) |
| 63 | + this.lengthStack = this.stack.length |
| 64 | + } |
| 65 | + |
| 66 | + getElement() { |
| 67 | + let element = this.stack.pop() |
| 68 | + this.lengthStack = this.stack.length |
| 69 | + return element |
| 70 | + } |
| 71 | + |
| 72 | + show() { |
| 73 | + console.log('Top') |
| 74 | + console.log('---') |
| 75 | + for (let i = this.stack.length - 1; i >= 0; i--) { |
| 76 | + if (i === this.stack.length - 1) { |
| 77 | + console.log(this.stack[i] + ' <- ultimo en entrar, primero en salir.') |
| 78 | + } |
| 79 | + console.log(this.stack[i]) |
| 80 | + } |
| 81 | + console.log('---') |
| 82 | + console.log('Bot') |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | +} |
| 87 | + |
| 88 | +let myStack = new Stack() |
| 89 | +console.log(myStack); |
| 90 | + |
| 91 | +myStack.addElement('elemento 1') |
| 92 | +myStack.addElement('elemento 2') |
| 93 | +myStack.addElement('elemento 3') |
| 94 | +myStack.addElement('elemento 4') |
| 95 | +myStack.addElement('elemento 5') |
| 96 | +myStack.show() |
| 97 | +console.log(myStack.lengthStack); |
| 98 | +console.log(myStack.getElement()); |
| 99 | +console.log(myStack.lengthStack); |
| 100 | +console.log(''); |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | +class Queue { |
| 107 | + |
| 108 | + constructor(initialElements = []) { |
| 109 | + this.queue = [...initialElements] |
| 110 | + this.lengthQueue= this.queue.length |
| 111 | + } |
| 112 | + |
| 113 | + addElement(element) { |
| 114 | + this.queue.push(element) |
| 115 | + this.lengthStack = this.queue.length |
| 116 | + } |
| 117 | + |
| 118 | + getElement() { |
| 119 | + let element = this.queue.shift() |
| 120 | + this.lengthStack = this.queue.length |
| 121 | + return element |
| 122 | + } |
| 123 | + |
| 124 | + show() { |
| 125 | + console.log('Top') |
| 126 | + console.log('---') |
| 127 | + for (let i = this.queue.length - 1; i >= 0; i--) { |
| 128 | + if (i === 0) { |
| 129 | + console.log(this.queue[i] + ' <- Primero en entrar, primero en salir.') |
| 130 | + continue |
| 131 | + } |
| 132 | + console.log(this.queue[i]) |
| 133 | + } |
| 134 | + console.log('---') |
| 135 | + console.log('Bot') |
| 136 | + } |
| 137 | + |
| 138 | + |
| 139 | +} |
| 140 | + |
| 141 | +let myQueue= new Queue() |
| 142 | +console.log(myQueue); |
| 143 | + |
| 144 | +myQueue.addElement('elemento 1') |
| 145 | +myQueue.addElement('elemento 2') |
| 146 | +myQueue.addElement('elemento 3') |
| 147 | +myQueue.addElement('elemento 4') |
| 148 | +myQueue.addElement('elemento 5') |
| 149 | +myQueue.show() |
| 150 | +console.log(myQueue.lengthStack); |
| 151 | +console.log(myQueue.getElement()); |
| 152 | +console.log(myQueue.lengthStack); |
| 153 | + |
0 commit comments