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 all the way to n the user's inputted number this will be known as Q Then an empty queue needs to be created called Q so that the compueter can switch over the prime numbers from Q into Q This is my js code: Node classfor the list
class Node
constructorvalue
this.value value;
this.next null;
Queue class using a list
class Queue
constructor
this.head null;
this.tail null;
Enqueue function using push
enqueuevalue
let node new Nodevalue;
if thishead null
this.head node;
this.tail node;
else
this.tail.next node;
this.tail node;
Dequeuefunction using shift
dequeue
if thishead null
return null;
else
let node this.head;
this.head this.head.next;
if thishead null
this.tail null;
return node.value;
Peekfunction to get the front element
peek
if thishead 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 sieveOfEratosthenesn
Create two queues
let Q new Queue;
let Q new Queue;
Fill Q with numbers from to n
for let i ; i n; i
Qenqueuei;
Initialize the iteration number
let iteration ;
Loop until Q is empty
while QisEmpty
Dequeue the first element of Q and call it X
let X Qdequeue;
Enqueue X to Q
QenqueueX;
Iterate over the remaining elements of Q
while QisEmpty
Dequeue the next element of Q and call it Y
let Y Qdequeue;
Check if Y is divisible by X
if Y X
If not, enqueue Y back to Q
QenqueueY;
Increment the iteration number
iteration;
Print the values of Q and Q
console.logIteration $iteration: Q $Qprint Q $Qprint;
Test the code with n
sieveOfEratosthenes;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
