I wrote a javascript file for Queue and then one for CircularQueue , when i was trying to use the functions already declared in Queue by using new , i could not get to call the Queue file, how can i use it in my new file …[I am currently using “this” and “new” but will soon quit using them if i can]… Thanks
My File Queue.js
function Queue() {
"use strict";
var items = [];
this.enqueue = function (elements) {
items.push(elements);
};
this.dequeue = function () {
return items.shift();
};
this.isEmpty = function () {
return items.length === 0;
};
this.size = function () {
return items.length;
};
this.clear = function () {
items = [];
};
this.print = function () {
console.log(items.toString());
};
}
My Other file where i want to use Queue.js , its called Circular.js
function hotPotato(nameList, num) {
"use strict";
var queue = new Queue();
for(var i = 0; i < nameList.length; i++) {
queue.enqueue(nameList[i]);
}
var eliminated = "";
while (queue.size() > 1) {
for(var j = 0; j < num; i += 1) {
queue.enqueue(queue.dequeue());
}
eliminated = queue.dequeue();
console.log(eliminated + ": has been removed");
}
return queue.dequeue();
}
Are you running your code on a browser? Because if that’s the case you can put two <script> elements on an HTML page (the <script> for Queue.js should go before that for Circular.js).
@jx2bandito - Thanks but when i am trying to run the circular file the error is not being able to find Queue, have you been able to access Queue.js from Circular.js ? If so kindly let me know how
@amitshrivastavafcc of course you can, the interpreter will parse your entire code from all files. You just have to watch out that you don’t include them in the wrong order. This can cause ‘hoisting’.
Also use your developer tools to check if all your files are found.