From 44aa7d85f684fe550ad80f8768c3ea0f1c62095a Mon Sep 17 00:00:00 2001 From: alvaro sanchez Date: Wed, 18 Oct 2023 09:15:07 +0200 Subject: [PATCH] terminado --- src/queue/queue-data-structure.js | 30 +++++++++++++---- src/queue/queue-dom.js | 54 ++++++++++++++++++++++++++----- src/stack/stack-data-structure.js | 31 ++++++++++++++---- 3 files changed, 94 insertions(+), 21 deletions(-) diff --git a/src/queue/queue-data-structure.js b/src/queue/queue-data-structure.js index 98ac0fd0..0b0a4b8e 100644 --- a/src/queue/queue-data-structure.js +++ b/src/queue/queue-data-structure.js @@ -5,24 +5,42 @@ class Queue { } canEnqueue() { - // ... your code goes here + if (this.queueControl.length >= this.MAX_SIZE) { + return false + } + return true } isEmpty() { - // ... your code goes here + if (this.queueControl.length === 0) { + return true + } + return false + } enqueue(item) { - // ... your code goes here + if (this.canEnqueue()) { + this.queueControl.push(item) + return this.queueControl + } else { + throw new Error('QUEUE_OVERFLOW') + } } + dequeue() { - // ... your code goes here + if (!this.isEmpty()) { + return this.queueControl.shift() + } + throw new Error('QUEUE_UNDERFLOW') } + + display() { - // ... your code goes here - } + return this.queueControl + } } // This is required to enable the automated tests, please ignore it. diff --git a/src/queue/queue-dom.js b/src/queue/queue-dom.js index 236675b7..bc46cc3c 100644 --- a/src/queue/queue-dom.js +++ b/src/queue/queue-dom.js @@ -10,38 +10,76 @@ const dequeue = document.querySelector('.btn-take-dequeue'); const queue = new Queue(); const clearQueueInput = () => { - // ... your code goes here + queueInput.value = ''; }; const generateListQueue = () => { - // ... your code goes here + warningTopQueue.style.display = 'none'; + warningBottomQueue.style.display = 'none'; + queueUL.innerHTML = ''; + let length = queue.display().length; + let size = queue.MAX_SIZE - length; + + queue.display().forEach(item => { + let li = document.createElement('li'); + li.className = 'active'; + li.innerText = item; + queueUL.appendChild(li); + }); + + for (let i = 0; i < size; i++) { + let li = document.createElement('li'); + li.className = 'inactive'; + li.innerHTML = ' '; + queueUL.appendChild(li); + } }; + generateListQueue(); + const generateWarningQueue = (type) => { if (type === 'underflow') { - // ... your code goes here + warningBottomQueue.style.display = 'block'; + warningBottomQueue.innerText = type; } else if (type === 'overflow') { - // ... your code goes here + warningTopQueue.style.display = 'block'; + warningTopQueue.innerText = type; } }; const addToQueue = () => { try { - // ... your code goes here + queue.enqueue(queueInput.value) + clearQueueInput(); + generateListQueue() } catch (error) { - // there was an overflow error, handle it + generateWarningQueue('overflow∫') } }; const removeFromQueue = () => { try { - // ... your code goes here + queue.dequeue() + generateListQueue() } catch (error) { - // there was an underflow error, handle it + generateWarningQueue('underflow') } }; addQueue.addEventListener('click', addToQueue); dequeue.addEventListener('click', removeFromQueue); +Contraer + + + + + + + + + + + + diff --git a/src/stack/stack-data-structure.js b/src/stack/stack-data-structure.js index 1106f6f3..76563b15 100644 --- a/src/stack/stack-data-structure.js +++ b/src/stack/stack-data-structure.js @@ -5,24 +5,41 @@ class Stack { } canPush() { - // ... your code goes here + if (this.stackControl.length < this.MAX_SIZE) { + return true + } + return false } + isEmpty() { - // ... your code goes here + if (this.stackControl.length === 0) { + return true + } + return false + } push(item) { - // ... your code goes here + if (this.canPush()) { + this.stackControl.push(item) + return this.stackControl + } else { + throw new Error('STACK_OVERFLOW') + } } pop() { - // ... your code goes here - } + if (!this.isEmpty()) { + return this.stackControl.pop() + } + throw new Error('STACK_UNDERFLOW') + } display() { - // ... your code goes here - } + + return this.stackControl + } } // This is required to enable the automated tests, please ignore it.