-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule-2-programming-project.html
186 lines (142 loc) · 4.15 KB
/
module-2-programming-project.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>CMPS 260: Module 2 Programming Project</title>
<style>* { font-family: monospace; }</style>
<script>
// NOTE: You must implement the data structures using the prototype approach.
// This is not what the book uses, so you have to convert it.
// See also: https://it.pointpark.edu/tutorials/no-prototype-vs-prototype/
// NOTE: Please review the following links regularly:
// https://it.pointpark.edu/tutorials/arrays-vs-objects/
// https://it.pointpark.edu/tutorials/no-prototype-vs-prototype/
// https://it.pointpark.edu/tutorials/implementation-vs-interface/
//---------//
// Project //
//---------//
console.log("Project");
// 1. Implement the stack data structure using the prototype.
function Queue () {
this.items = [];
this.count = 0;
}
Queue.prototype.enqueue = function(element) {
this.items.push(element);
}
Queue.prototype.dequeue = function() {
if (this.isEmpty()) {
return undefined;
}
else {
this.items.pop();
}
}
Queue.prototype.size = function() {
return this.items.length;
}
Queue.prototype.isEmpty = function() {
return (this.items.length === 0);
}
// 2. It is possible to use a stack to check if the number of parentheses in a
// string is balanced, meaning there are as many opening parentheses as
// closing ones. In addition, we can also make sure that each opening
// parenthesis precedes a closing parenthesis. Implement this algorithm.
// HINT: When encountering '(' push to the stack and when encountering ')'
// pop from the stack.
function isBalanced(str) {
// check the parentheses in str
const stack = new Queue();
const brackets = "()";
for (i = 0; i < str.length; i++) {
if (str[i] === "(") {
stack.enqueue(str[i]);
} else if (str[i] === ")") {
stack.dequeue();
}
}
if (stack.size() === 0) {
console.log("The brackets are balanced.");
return true;
}
else {
console.log("The brackets are NOT balanced.");
return false;
}
}
// 3. Write a simple test program that shows your implementation in the
// previous question works.
var code = 'this.prototype.size = function() {return this.items.length(;}';
var lineOfCode = isBalanced(code);
// 4. Implement the queue data structure using the prototype.
// 5. Create a queue that stores edibles, that can be either fruits or
// vegetables. Use the constructor below to create the edible and store a
// few of each kind in the queue (at least 3 of each).
function Edible(name, isFruit) {
this.name = name;
this.isFruit = isFruit; // if not fruit, it must be a vegetable
this.toString = function() {
return "Edible: " + this.name + " Fruit: " + this.isFruit;
};
}
function Queue () {
this.items = [];
this.count = 0;
}
Queue.prototype.enqueue = function(name, isFruit) {
var newQ = new Edible(name, isFruit);
var added = false;
this.items.push(newQ);
this.count++;
}
Queue.prototype.dequeue = function() {
if (this.isEmpty()) {
return undefined;
}
else {
this.items.shift();
}
}
Queue.prototype.size = function() {
return this.items.length;
}
Queue.prototype.isEmpty = function() {
return (this.items.length === 0);
}
var edibleQueue = new Queue();
edibleQueue.enqueue('Broccoli', false);
edibleQueue.enqueue('Apples', true);
edibleQueue.enqueue('Peas', false);
edibleQueue.enqueue('Oranges', true);
var foodItem = objedibleQueue.dequeue();
//console.log(foodItem.toString());
//console.log(foodItem);
console.log(edibleQueue.items);
console.log(edibleQueue.count);
// 6. Create two more queues: one for fruits and one for vegetables. For this
// question, only create them (and leave them empty).
//psuedocode...
/*
var fruitQueue = new Queue();
if (isFruit === true) {
this.items.push();
}
else
{
console.log("Not a fruit.");
break;
};
var vegetableQueue = new Queue();
//psuedocode...
if (isFruit === false) {
}
*/
// 7. Dequeue all the elements from the edible queue and enqueue them in the
// appropriate queue, either for fruits or vegetables.
// 8. Print the final contents to the console, showing that your code works.
</script>
</head>
<body>
See console!
</body>
</html>