JSFiddle - React, Tailwind, and code Playground

by Jeff Santos

HTML

Please Enter Value N
<br/>
<input type="textbox" id="nValue" value="" />

<input type="button" id="enQueue" value="Fill Queue" onClick="enQueue();" />
<input type="button" id="deQueue" value="Perform Sieve of Eratosthenes" onClick="deQueue();" />


<p id='output'></p>

JavaScript

var val = '';
var iteration = 0;
var Node = function(_content) {
  this.next = null;
  this.previous = null;
  this.content = _content;

}

var Queue = function() {
  this.front = null;
  this.back = null;


  this.push = function(_content) {

    if (this.front == null) {
      this.front = new Node(_content);
      this.back = this.front;
      return this;
    }


    var addedNode = new Node(_content);
    addedNode.previous = this.back;
    this.back.next = addedNode;
    this.back = addedNode;
    return this;
  }


  this.removeFront = function() {
    if (this.front == null) {
      return null;
    }
    var remove = this.front.content;

    if (this.back == this.front) {
      this.front = null;
      this.back = null;
      return remove;
    } else {
      this.front = this.front.next;
      this.front.previous = null;
      return remove;
    }
  }

  this.pop2 = function() {
    if (this.front == null) {

      return null;
    }
    var remove = this.back.content;
    if (this.back == this.front) {
      this.front = null;
      this.back = null;
      return a;
    } else {
      this.back = this.back.previous;
      this.back.next = null;
      return remove;
    }
  }


  this.toString = function() {
    var str = "";
    var node = this.front;
    if (this.front == null) {
      str = "Queue is Empty";
    }
    while (node != null) {
      str += node.content + "&nbsp";
      node = node.next;
    }
    return str;
  }


  this.cnt = function() {
    var c = 0;
    var node = this.front;
    while (node != null) {
      node = node.next;
      c = c + 1;
    }
    return c;
  }
}


function deQueue() {
  if (qOne.cnt() > 0) {


    qTwo = new Queue();


    var vh = qOne.removeFront();
    val = vh;
    listOut.push(val);

    while (vh != null) {

      var x = vh % val;
      if (x >= 1) {
        qTwo.push(vh);
      } else {
        listRemoved.push(vh);
      }
      vh = qOne.removeFront();
    }


    qOne = qTwo;
    iteration++;
    d...