From 8f959d9c4dfc5387d68b81bcd2ca9a1ad7d47d10 Mon Sep 17 00:00:00 2001 From: Yujiro Akihiro Date: Wed, 17 Jul 2024 14:50:10 +0200 Subject: [PATCH] #02 should be ok? --- src/queue/queue-data-structure.js | 33 +++++++++------ src/queue/queue-dom.js | 67 +++++++++++++------------------ 2 files changed, 50 insertions(+), 50 deletions(-) diff --git a/src/queue/queue-data-structure.js b/src/queue/queue-data-structure.js index 98ac0fd0..89faf939 100644 --- a/src/queue/queue-data-structure.js +++ b/src/queue/queue-data-structure.js @@ -1,29 +1,38 @@ -class Queue { +class Stack { constructor() { - this.queueControl = []; + this.stackControl = []; this.MAX_SIZE = 10; } - canEnqueue() { - // ... your code goes here + canPush() { + return this.stackControl.length < this.MAX_SIZE; } isEmpty() { - // ... your code goes here + return this.stackControl.length === 0; } - enqueue(item) { - // ... your code goes here + push(item) { + if (this.canPush()) { + this.stackControl.push(item); + return this.stackControl; + } else { + throw new Error('STACK_OVERFLOW'); + } } - dequeue() { - // ... your code goes here + pop() { + 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 = Queue; +if (typeof module !== 'undefined') module.exports = Stack; diff --git a/src/queue/queue-dom.js b/src/queue/queue-dom.js index 236675b7..d5a0be9b 100644 --- a/src/queue/queue-dom.js +++ b/src/queue/queue-dom.js @@ -1,47 +1,38 @@ -const queueUL = document.querySelector('.list-queue'); -const queueInput = document.querySelector('.queue-input'); -const warningTopQueue = document.querySelector('#queue-container .warning-top'); -const warningBottomQueue = document.querySelector( - '#queue-container .warning-bottom' -); -const addQueue = document.querySelector('.btn-add-queue'); -const dequeue = document.querySelector('.btn-take-dequeue'); - -const queue = new Queue(); - -const clearQueueInput = () => { - // ... your code goes here -}; +class Queue { + constructor() { + this.queueControl = []; + this.MAX_SIZE = 10; + } -const generateListQueue = () => { - // ... your code goes here -}; + canEnqueue() { + return this.queueControl.length < this.MAX_SIZE; + } -generateListQueue(); + isEmpty() { + return this.queueControl.length === 0; + } -const generateWarningQueue = (type) => { - if (type === 'underflow') { - // ... your code goes here - } else if (type === 'overflow') { - // ... your code goes here + enqueue(item) { + if (this.canEnqueue()) { + this.queueControl.push(item); + return this.queueControl; + } else { + throw new Error('QUEUE_OVERFLOW'); + } } -}; -const addToQueue = () => { - try { - // ... your code goes here - } catch (error) { - // there was an overflow error, handle it + dequeue() { + if (!this.isEmpty()) { + return this.queueControl.shift(); + } else { + throw new Error('QUEUE_UNDERFLOW'); + } } -}; -const removeFromQueue = () => { - try { - // ... your code goes here - } catch (error) { - // there was an underflow error, handle it + display() { + return this.queueControl; } -}; +} -addQueue.addEventListener('click', addToQueue); -dequeue.addEventListener('click', removeFromQueue); +// This is required to enable the automated tests, please ignore it. +if (typeof module !== 'undefined') module.exports = Queue;