diff --git a/src/queue/queue-data-structure.js b/src/queue/queue-data-structure.js index 98ac0fd0..022b4618 100644 --- a/src/queue/queue-data-structure.js +++ b/src/queue/queue-data-structure.js @@ -5,25 +5,78 @@ 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 + + try{ + if(this.canEnqueue()) + {this.queueControl.unshift(item)} + else + {throw new Error("STACK_OVERFLOW")}; + } + catch(error){ + console.log(error); + } + + + } dequeue() { - // ... your code goes here + + try { + if(!this.isEmpty()) + {this.queueControl.shift();} + else + {throw new Error("STACK_UNDERRFLOW");} + } catch (error) { + console.log(error); + } + } + 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; +/* + +const QueueA = new Queue(); + +for(let i=QueueA.display().length;i0;i--){ + QueueA.dequeue(); +} + +QueueA.dequeue(); +console.log(QueueA.display()); +*/ + +//const newQueue = new Queue(); + +//console.log(newQueue.display().length); \ No newline at end of file diff --git a/src/queue/queue-dom.js b/src/queue/queue-dom.js index 236675b7..37383e9f 100644 --- a/src/queue/queue-dom.js +++ b/src/queue/queue-dom.js @@ -10,38 +10,89 @@ 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 = () => { + queue.enqueue(queueInput.value); + clearQueueInput(); + generateListQueue(); + ///generateWarningQueue(); + /* try { + // ... your code goes here } 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); + + + + +/* +class Food{ + + constructor(carbs,prot,fat){ + this.carbs = carbs; + this.prot = prot; + this.fat = fat; + } + + calorieC = this.carbs * 4; + calorieP = this.prot * 4; + calorieF = this.fat * 9; + + TotalCalories = this.calorieC + this.calorieP + this.calorieF; + + printcalories(){ + console.log(`This food has P: ${this.prot} C: ${this.carbs} F: ${this.fat} and has a caloric total amount of ${this.TotalCalories}`); + } +}*/ \ No newline at end of file diff --git a/src/stack/stack-data-structure.js b/src/stack/stack-data-structure.js index 1106f6f3..c713c554 100644 --- a/src/stack/stack-data-structure.js +++ b/src/stack/stack-data-structure.js @@ -5,25 +5,72 @@ class Stack { } canPush() { + return this.stackControl.length < this.MAX_SIZE; // ... your code goes here } isEmpty() { + return this.stackControl.length === 0; // ... your code goes here } push(item) { - // ... your code goes here + + try { + if(this.canPush()) + {this.stackControl.push(item)} + else + {throw new Error("STACK_OVERFLOW")} + } catch (error) { + console.log(error); + } + + } pop() { - // ... your code goes here + try{ + if(!this.isEmpty()) + {this.stackControl.pop();} + else + {throw new Error("STACK_UNDERRFLOW")} + } + catch(error){ + console.log(error); + } + } 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; + +/* +const StackA = new Stack(); + +for(let i=StackA.display().length;i0;i--){ + StackA.pop(); +} +StackA.pop(); +console.log(StackA.display()); +*/ \ No newline at end of file