Question: Why is my js code not working the jsfiddle just won't respond. I also need help with the html for this code so that a

Why is my js code not working the jsfiddle just won't respond. I also need help with the html for this code so that a user can enter a number so that the first queue starts from 2 all the way to n (the user's inputted number) this will be known as Q1. Then an empty queue needs to be created called Q2 so that the compueter can switch over the prime numbers from Q1 into Q2. This is my js code: //Node classfor 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;
}
}
// Dequeuefunction 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;
}
}
// Peekfunction to get the front element
peek(){
if (this.head === null){
return null;
} else {
return this.head.value;
}
}
//IsEmptyfunction to checkif the queue is empty
isEmpty
()
{
return this.head === null;
}
// Print function to display the queue
}
// 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
console.log(`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!