diff --git a/SpecRunner.html b/SpecRunner.html index 0e26d807..5c5ab30c 100644 --- a/SpecRunner.html +++ b/SpecRunner.html @@ -19,7 +19,7 @@ - + diff --git a/index.html b/index.html index 3fab1ac7..b22f567e 100644 --- a/index.html +++ b/index.html @@ -83,10 +83,9 @@

Queue

- - - - - + + + + diff --git a/src/queue/queue-data-structure.js b/src/queue/queue-data-structure.js index 98ac0fd0..a035194d 100644 --- a/src/queue/queue-data-structure.js +++ b/src/queue/queue-data-structure.js @@ -5,25 +5,35 @@ class Queue { } canEnqueue() { - // ... your code goes here + return this.queueControl.length < this.MAX_SIZE; } isEmpty() { - // ... your code goes here + return !this.queueControl.length; } 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. if (typeof module !== 'undefined') module.exports = Queue; + diff --git a/src/queue/queue-dom.js b/src/queue/queue-dom.js index 236675b7..db5896f8 100644 --- a/src/queue/queue-dom.js +++ b/src/queue/queue-dom.js @@ -1,45 +1,67 @@ + 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 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 + queueInput.value = ''; }; const generateListQueue = () => { - // ... your code goes here + warningTopQueue.style.display = 'none'; + warningBottomQueue.style.display = 'none'; + queueUL.innerHTML = ''; + let len = queue.display().length; + let size = queue.MAX_SIZE - len; + + 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 = 'innactive'; + 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'); } }; diff --git a/src/stack/stack-data-structure.js b/src/stack/stack-data-structure.js index 1106f6f3..cd53b7eb 100644 --- a/src/stack/stack-data-structure.js +++ b/src/stack/stack-data-structure.js @@ -5,23 +5,32 @@ class Stack { } canPush() { - // ... your code goes here + return this.stackControl.length < this.MAX_SIZE; } isEmpty() { - // ... your code goes here + return !this.stackControl.length; } 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; } }