JSFiddle - React, Tailwind, and code Playground

by JInks Peng

JavaScript

function Queue() {
   this.dataStore = [];
   this.enqueue = enqueue;
   this.dequeue = dequeue;
   this.front = front;
   this.back = back;
   this.count = count;
   this.toString = toString;
   this.isEmpty = isEmpty;
}
function enqueue(elem) {
   this.dataStore.push(elem);
}
function dequeue() {
   return this.dataStore.shift();
}
function front() {
   return this.dataStore[0];
}
function back() {
   return this.dataStore[this.dataStore.length - 1];
}
function toString() {
   var retStr = "";
   for(var i = 0; i < this.dataStore.length; ++i) {
      retStr += this.dataStore[i] + "\n";
   }
   return retStr;
}
function count() {
   return this.dataStore.length;
}
function isEmpty() {
   if(this.dataStore.length === 0) {
      return true;
   } else {
      return false;
   }
}


function Patient(name,code) {
   this.name = name;
   this.code = code;
}
// code smaller has priority;

function dequeue() {
   var priority = this.dataStore[0].code, number;
   for(var i = 0; i < this.count(); ++i) {
      if(this.dataStore[i].code < priority) {
         priority = this.dataStore[i].code;
         number = i;
      } 
   }
   return this.dataStore.splice(number,1);
}

function toString() {
   var str = "";
   for(var i = 0; i < this.count(); ++i) {
      str += "name: "+ this.dataStore[i].name + " code: " + this.dataStore[i].code + "\n";
   }
   return str;
}

var p = new Patient("Smith",5);
var ed = new Queue();
ed.enqueue(p);
p = new Patient("Jones",4);
ed.enqueue(p);
p = new Patient("Frehrenbach",6);
ed.enqueue(p);
p = new Patient("Brown",1);
ed.enqueue(p);
p = new Patient("Ingram",1);
ed.enqueue(p);
console.log(ed.toString());

var seen = ed.dequeue();
console.log("Patient being treated: " + seen[0].name);
console.log("Patient waiting to be seen:\n" + ed.toString());

var seen = ed.dequeue();
console.log("Patient being treated: " + seen[0].name);
console.log("Patient waiting to be seen:\n" + ed.toString());