From f2d92ca2e1675b772ba77bc1a557680167aed859 Mon Sep 17 00:00:00 2001 From: Christian Doeller Date: Sun, 17 Dec 2023 19:12:35 +0100 Subject: [PATCH 1/3] finished logic --- src/queue/queue-data-structure.js | 22 ++++++++++++++++++++++ src/stack/stack-data-structure.js | 27 +++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/queue/queue-data-structure.js b/src/queue/queue-data-structure.js index 98ac0fd0..7e850525 100644 --- a/src/queue/queue-data-structure.js +++ b/src/queue/queue-data-structure.js @@ -6,22 +6,44 @@ class Queue { canEnqueue() { // ... your code goes here + if (this.queueControl.length < this.MAX_SIZE){ + return true; + } else { + return false; + } } isEmpty() { // ... your code goes here + if (this.queueControl.length === 0){ + return true; + }else { + return false; + } } enqueue(item) { // ... your code goes here + if (this.canEnqueue() === false){ + throw new Error('QUEUE_OVERFLOW'); + } else { + this.queueControl.push(item); + return this.queueControl; + } } dequeue() { // ... your code goes here + if (this.isEmpty() === true){ + throw new Error('QUEUE_UNDERFLOW'); + } else { + return this.queueControl.shift(); + } } display() { // ... your code goes here + return this.queueControl; } } diff --git a/src/stack/stack-data-structure.js b/src/stack/stack-data-structure.js index 1106f6f3..f14534d7 100644 --- a/src/stack/stack-data-structure.js +++ b/src/stack/stack-data-structure.js @@ -6,24 +6,47 @@ class Stack { canPush() { // ... your code goes here + if (this.stackControl.length < this.MAX_SIZE) { + return true; + } else { + return false; + } } isEmpty() { // ... your code goes here + if (this.stackControl.length === 0) { + return true; + } else { + return false; + } } push(item) { // ... your code goes here + if (this.canPush() === false) { + throw new Error("STACK_OVERFLOW"); + } else { + this.stackControl.push(item); + return this.stackControl; + } } pop() { // ... your code goes here + if (this.isEmpty() === true) { + throw new Error("STACK_UNDERFLOW"); + } else { + let lastItem = this.stackControl.pop(); + return lastItem; + } } 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 a07e040674925887d1831be434ea81fe36f3c56c Mon Sep 17 00:00:00 2001 From: Christian Doeller Date: Sun, 17 Dec 2023 21:16:05 +0100 Subject: [PATCH 2/3] finished DOM --- src/queue/queue-data-structure.js | 2 ++ src/queue/queue-dom.js | 39 +++++++++++++++++++++++++++++-- src/stack/stack-data-structure.js | 6 +++-- src/stack/stack-dom.js | 9 ++++--- 4 files changed, 49 insertions(+), 7 deletions(-) diff --git a/src/queue/queue-data-structure.js b/src/queue/queue-data-structure.js index 7e850525..a230a05d 100644 --- a/src/queue/queue-data-structure.js +++ b/src/queue/queue-data-structure.js @@ -25,6 +25,7 @@ class Queue { enqueue(item) { // ... your code goes here if (this.canEnqueue() === false){ + //return ('Queue Overflow'); throw new Error('QUEUE_OVERFLOW'); } else { this.queueControl.push(item); @@ -35,6 +36,7 @@ class Queue { dequeue() { // ... your code goes here if (this.isEmpty() === true){ + //return ('Queue Underflow'); throw new Error('QUEUE_UNDERFLOW'); } else { return this.queueControl.shift(); diff --git a/src/queue/queue-dom.js b/src/queue/queue-dom.js index 236675b7..9b77b9e2 100644 --- a/src/queue/queue-dom.js +++ b/src/queue/queue-dom.js @@ -11,37 +11,72 @@ 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"; + // reset UL to be filled with new information + queueUL.innerHTML = ''; + let queueLength = queue.display().length; + let emptySlots = queue.MAX_SIZE - queueLength; + queue.display().forEach((queueElement)=>{ + let li = document.createElement("li"); + li.className = "active"; + li.innerHTML = queueElement; + queueUL.appendChild(li); + }); + for (let i = 0; i < emptySlots; i++){ + let li = document.createElement("li"); + li.className = 'inactive'; + li.innerHTML = ' '; + queueUL.appendChild(li); + } }; - +// call function generateListQueue(); const generateWarningQueue = (type) => { if (type === 'underflow') { // ... your code goes here + console.log ("underflow") + warningBottomQueue.style.display = "block"; + warningBottomQueue.textContent = type; } else if (type === 'overflow') { // ... your code goes here + console.log ("overflow") + warningTopQueue.style.display = "block"; + warningTopQueue.textContent = type; + } }; const addToQueue = () => { try { // ... your code goes here + queue.enqueue(queueInput.value); + clearQueueInput(); + generateListQueue(); + //console.log (queueInput.value); } catch (error) { // there was an overflow error, handle it + generateWarningQueue('overflow'); } }; const removeFromQueue = () => { try { // ... your code goes here + queue.dequeue(); + generateListQueue(); + //console.log ("dequeue"); } catch (error) { // there was an underflow error, handle it + generateWarningQueue('underflow'); } }; addQueue.addEventListener('click', addToQueue); -dequeue.addEventListener('click', removeFromQueue); +dequeue.addEventListener('click', removeFromQueue); \ No newline at end of file diff --git a/src/stack/stack-data-structure.js b/src/stack/stack-data-structure.js index f14534d7..b72c03d9 100644 --- a/src/stack/stack-data-structure.js +++ b/src/stack/stack-data-structure.js @@ -25,7 +25,8 @@ class Stack { push(item) { // ... your code goes here if (this.canPush() === false) { - throw new Error("STACK_OVERFLOW"); + return ('Stack Overflow'); + //throw new Error("STACK_OVERFLOW"); } else { this.stackControl.push(item); return this.stackControl; @@ -35,7 +36,8 @@ class Stack { pop() { // ... your code goes here if (this.isEmpty() === true) { - throw new Error("STACK_UNDERFLOW"); + return ('Stack Underflow'); + //throw new Error("STACK_UNDERFLOW"); } else { let lastItem = this.stackControl.pop(); return lastItem; diff --git a/src/stack/stack-dom.js b/src/stack/stack-dom.js index db661587..627fce0a 100644 --- a/src/stack/stack-dom.js +++ b/src/stack/stack-dom.js @@ -1,6 +1,6 @@ const stackList = document.getElementById('stack-list'); const stackInput = document.getElementById('stack-input'); -const container = document.getElementById('container'); +//const container = document.getElementById('container'); const warningTopStack = document.querySelector('#stack-container .warning-top'); const warningBottomStack = document.querySelector('#stack-container .warning-bottom'); const addStackBtn = document.getElementById('add-stack'); @@ -8,7 +8,7 @@ const takeStackBtn = document.getElementById('take-stack'); const newStack = new Stack(); -const clearStackInput = () => { +const cslearStackInput = () => { stackInput.value = ''; }; @@ -31,10 +31,12 @@ const renderListStack = () => { stackList.appendChild(li); } }; +// function call renderListStack(); -const generateWarningStack = type => { +const generateWarningStack = (type) => { if (type === 'underflow') { + console.log ("UNDERFLOW HIT"); warningBottomStack.style.display = 'block'; warningBottomStack.innerText = type; } else if (type === 'overflow') { @@ -55,6 +57,7 @@ const addToStack = () => { const removeFromStack = () => { if (newStack.pop() === 'Stack Underflow') { generateWarningStack('underflow'); + console.log ("UNDERFLOW HIT"); } else { renderListStack(); } From 194d2a5ffae414315542d08b9b1026af059152f0 Mon Sep 17 00:00:00 2001 From: Christian Doeller Date: Sun, 17 Dec 2023 23:19:29 +0100 Subject: [PATCH 3/3] finished lab --- src/stack/stack-dom.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/stack/stack-dom.js b/src/stack/stack-dom.js index 627fce0a..20aa5b66 100644 --- a/src/stack/stack-dom.js +++ b/src/stack/stack-dom.js @@ -8,7 +8,7 @@ const takeStackBtn = document.getElementById('take-stack'); const newStack = new Stack(); -const cslearStackInput = () => { +const clearStackInput = () => { stackInput.value = ''; }; @@ -46,6 +46,7 @@ const generateWarningStack = (type) => { }; const addToStack = () => { + console.log ("called") if (newStack.push(stackInput.value) === 'Stack Overflow') { generateWarningStack('overflow'); } else {