Skip to content

PT-WD Christian Doeller #1137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/queue/queue-data-structure.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,46 @@ 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){
//return ('Queue Overflow');
throw new Error('QUEUE_OVERFLOW');
} else {
this.queueControl.push(item);
return this.queueControl;
}
}

dequeue() {
// ... your code goes here
if (this.isEmpty() === true){
//return ('Queue Underflow');
throw new Error('QUEUE_UNDERFLOW');
} else {
return this.queueControl.shift();
}
}

display() {
// ... your code goes here
return this.queueControl;
}
}

Expand Down
39 changes: 37 additions & 2 deletions src/queue/queue-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '&nbsp;';
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);
29 changes: 27 additions & 2 deletions src/stack/stack-data-structure.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,49 @@ 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) {
return ('Stack Overflow');
//throw new Error("STACK_OVERFLOW");
} else {
this.stackControl.push(item);
return this.stackControl;
}
}

pop() {
// ... your code goes here
if (this.isEmpty() === true) {
return ('Stack Underflow');
//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;
8 changes: 6 additions & 2 deletions src/stack/stack-dom.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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') {
Expand All @@ -44,6 +46,7 @@ const generateWarningStack = type => {
};

const addToStack = () => {
console.log ("called")
if (newStack.push(stackInput.value) === 'Stack Overflow') {
generateWarningStack('overflow');
} else {
Expand All @@ -55,6 +58,7 @@ const addToStack = () => {
const removeFromStack = () => {
if (newStack.pop() === 'Stack Underflow') {
generateWarningStack('underflow');
console.log ("UNDERFLOW HIT");
} else {
renderListStack();
}
Expand Down