Question: Please help! Why is this code not working at all? Is there an infinite loop? I need help making the html for this javascript queue

Please help! Why is this code not working at all? Is there an infinite loop? I need help making the html for this javascript queue that is a augemented list (Not An Array). You will fill the first list with numbers consecutively numbered from
2 to n,where n is entered by the user
(we will call this Q1). Create a second empty queue called Q2. Here is the algorithm;
Dequeue 1st element in Q1(which will be 2).You will need to remember the value of this element we will call it X 2.Enqueue this element into Q2(Q2 is the list of primes)3. Iterate and Dequeue each successive element of Q1
If the value is divisible by X,go to the next element if the value is not divisible by X enqueue back onto Q1,and go to the next element. Print the values of Q1 and Q2after each time through. When done go back to the beginning of the Q1 and repeat steps 1-3(the first value will be 3 the second time around)
Sample output with input 10 Iteration 0: Q1=2345678910,Q2=,Iteration 1: Q1=3579,Q2=2,Iteration 2: Q1=57,Q2=23 Iteration 3: Q1=7,2=235 Iteration 4: Q1=, Q2=2357 This is the code: // Node class for the list
class Node {
constructor (value){
this.value = value;
this.next = null;
}
}
// Queue class using a list
class Queue {
constructor (){
this.head = null;
this.tail = null;
}
// Enqueue function using push()
enqueue (value){
let node = new Node (value);
if (this.head === null){
this.head = node;
this.tail = node;
} else {
this.tail.next = node;
this.tail = node;
}
}
// Dequeue function using shift()
dequeue (){
if (this.head === null){
return null;
} else {
let node = this.head;
this.head = this.head.next;
if (this.head === null){
this.tail = null;
}
return node.value;
}
}
// Peek function to get the front element
peek (){
if (this.head === null){
return null;
} else {
return this.head.value;
}
}
// IsEmpty function to check if the queue is empty
isEmpty (){
return this.head === null;
}
// Print function to display the queue
print (){
let node = this.head;
let result ="";
while (node !== null){
result += node.value +"";
node = node.next;
return result;
}
}
}
// Sieve of Eratosthenes algorithm using two queues
function sieveOfEratosthenes(n){
// Create two queues
let Q1= new Queue ();
let Q2= new Queue ();
// Fill Q1 with numbers from 2 to n
for (let i =2; i <= n; i++){
Q1.enqueue (i);
}
// Initialize the iteration number
let iteration =0;
// Loop until Q1 is empty
while (!Q1.isEmpty ()){
// Dequeue the first element of Q1 and call it X
let X = Q1.dequeue ();
// Enqueue X to Q2
Q2.enqueue (X);
// Iterate over the remaining elements of Q1
while (!Q1.isEmpty ()){
// Dequeue the next element of Q1 and call it Y
let Y = Q1.dequeue ();
// Check if Y is divisible by X
if (Y % X !==0){
// If not, enqueue Y back to Q1
Q1.enqueue (Y);
}
}
// Increment the iteration number
iteration++;
// Print the values of Q1 and Q2
const outputDiv = document.getElementById('output');
outputDiv.innerHTML +=`Iteration ${iteration}: Q1= ${Q1.print()} Q2= ${Q2.print()}`;
}
}
// Test the code with n =10
sieveOfEratosthenes (10);

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!