From 6138cf62c668298bb7da69a37507203afe0ffef8 Mon Sep 17 00:00:00 2001 From: Eduardo Botelho Date: Thu, 7 Dec 2023 20:17:05 +0000 Subject: [PATCH 1/3] stack done --- src/stack/stack-data-structure.js | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/stack/stack-data-structure.js b/src/stack/stack-data-structure.js index 1106f6f3..3bd6d0e9 100644 --- a/src/stack/stack-data-structure.js +++ b/src/stack/stack-data-structure.js @@ -5,25 +5,34 @@ class Stack { } canPush() { - // ... your code goes here + return this.stackControl.length < this.MAX_SIZE; } isEmpty() { - // ... your code goes here + return this.stackControl.length === 0; } 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(); + } else { + throw new Error("STACK_UNDERFLOW"); + } } display() { - // ... your code goes here - } + return this.stackControl; + } } // This is required to enable the automated tests, please ignore it. -if (typeof module !== 'undefined') module.exports = Stack; +if (typeof module !== "undefined") module.exports = Stack; From 80999bb14e28fa2daf03deac5f5f4765ce31234c Mon Sep 17 00:00:00 2001 From: Eduardo Botelho Date: Thu, 7 Dec 2023 20:31:33 +0000 Subject: [PATCH 2/3] queue done --- src/queue/queue-data-structure.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/queue/queue-data-structure.js b/src/queue/queue-data-structure.js index 98ac0fd0..869f8545 100644 --- a/src/queue/queue-data-structure.js +++ b/src/queue/queue-data-structure.js @@ -5,24 +5,33 @@ class Queue { } canEnqueue() { - // ... your code goes here + return this.queueControl.length < this.MAX_SIZE; } isEmpty() { - // ... your code goes here + return this.queueControl.length === 0; } 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(); + } else { + throw new Error("QUEUE_UNDERFLOW"); + } } display() { - // ... your code goes here - } + return this.queueControl; + } } // This is required to enable the automated tests, please ignore it. From 5872bd7859e6df9782ad47622493ad6d0f68df82 Mon Sep 17 00:00:00 2001 From: Eduardo Botelho Date: Fri, 8 Dec 2023 12:54:58 +0000 Subject: [PATCH 3/3] done; fixed stack dom and implemented queue dom --- src/queue/queue-dom.js | 36 ++++++++++++++++++++++++++++++------ src/stack/stack-dom.js | 15 ++++++++------- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/queue/queue-dom.js b/src/queue/queue-dom.js index 236675b7..54e76de3 100644 --- a/src/queue/queue-dom.js +++ b/src/queue/queue-dom.js @@ -10,36 +10,60 @@ 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 - } else if (type === 'overflow') { - // ... your code goes here + if (type === "underflow") { + warningBottomQueue.style.display = "block"; + warningBottomQueue.innerText = type; + } else if (type === "overflow") { + warningTopQueue.style.display = "block"; + warningTopQueue.innerText = type; } }; const addToQueue = () => { try { // ... your code goes here + queue.enqueue(queueInput.value); + generateListQueue(); } catch (error) { // there was an overflow error, handle it + generateWarningQueue("overflow"); } }; const removeFromQueue = () => { try { // ... your code goes here + queue.dequeue(queueInput.value); + generateListQueue(); } catch (error) { // there was an underflow error, handle it + generateWarningQueue("underflow"); } }; diff --git a/src/stack/stack-dom.js b/src/stack/stack-dom.js index db661587..6963a9b2 100644 --- a/src/stack/stack-dom.js +++ b/src/stack/stack-dom.js @@ -44,19 +44,20 @@ const generateWarningStack = type => { }; const addToStack = () => { - if (newStack.push(stackInput.value) === 'Stack Overflow') { - generateWarningStack('overflow'); - } else { - clearStackInput(); + try { + newStack.push(stackInput.value); renderListStack(); + } catch (error) { + generateWarningStack("overflow"); } }; const removeFromStack = () => { - if (newStack.pop() === 'Stack Underflow') { - generateWarningStack('underflow'); - } else { + try { + newStack.pop(); renderListStack(); + } catch (error) { + generateWarningStack("underflow"); } };