JSFiddle - React, Tailwind, and code Playground

by hesster92

HTML

Enter a number:<br>
<input type="input" id="myNumber">
<br>
<input type="button" onclick="RunSieve()" value="Submit"><br><br> Output: <br>
<p id='output'></p>

JavaScript

function Node(content) {
  this.next = null;
  this.last = null;
  this.content = content;
}

function myQueue() {
  this.head = null;
  this.tail = null;
}

myQueue.prototype.toString = function() {
  var str = "";
  var node = this.head;

  while (node != null) {
    str += node.content + ",";
    node = node.next;
  }
  return str;
}


myQueue.prototype.isEmpty = function() {
  if (this.head == null) {
    return true;
  } else return false;
}

myQueue.prototype.dequeue = function() {
  var n;
  if (this.head !== null) {
    n = this.head.content;
    this.head = this.head.next;
  }
  return n;
}

myQueue.prototype.enqueue = function(content) {
  var n = new Node(content);

  if (this.head === null) {
    this.head = n;
    this.tail = n;
  } else {
    this.tail.next = n;
    this.tail = n;
  }
}

function CreateQueuebySize(size) {
  var i = 2;
  var q = new myQueue();
  for (i = 2; i < size + 1; i++) {
    q.enqueue(i);

  }
  return q;
}

function RunSieve() {

  var myNumber = Number(document.getElementById("myNumber").value);
  var q1 = CreateQueuebySize(myNumber);
  var q2 = new myQueue();
  var str1 = "";
  var str2 = "";
  var str = "";
  var count = 0;


  while (!q1.isEmpty()) {
    str = str + "Iteration " + String(count) + "		Q1: " + q1.toString() + "------------Q2:	 " + q2.toString() + " <br>";
    document.getElementById('output').innerHTML = str;
    var prime = q1.dequeue();
    q2.enqueue(prime);
    var temp = new myQueue();
    count = count + 1;
    while (!q1.isEmpty()) {
      var a = q1.dequeue();
      if (a % prime != 0) {
        temp.enqueue(a);
      }
    }
    q1 = temp;
  }
  str = str + "Iteration " + String(count) + "		Q1: " + q1.toString() + "------------Q2:	 " + q2.toString() + " <br>";
  document.getElementById('output').innerHTML = str;
}