Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

***Please use jsfiddle.net to create the code - post HTML and Javascript code -

ID: 3795708 • Letter: #

Question

***Please use jsfiddle.net to create the code - post HTML and Javascript code - make sure to use "no wrap in head" or "no wrap in body" under the javascript settings. Use a queue or use a list to create this code, NOT AN ARRAY. Thank you!***

You are going to create a Queue. (alternately you can create a list and simply implement enqueue and dequeue functions in the List - that will technically make it a queue). 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). When creating your Queue object use the correct function names for enqueue and dequeue functions. Again - sorry, cannot use an Javascript array in your implementation - you need to implement enqueue and dequeue.

Create a second empty Queue Q2.

Once you have the first Queue filled we are going to use a technique called Sieve of Eratosthenes which uses first queue to fill the second queue. You will need to look at the algorithm for this https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

Here is the algorithm;

1. 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, go to the next element.

4. Print the values of Q1 and Q2 after each time through.

5. 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 = 2 3 4 5 6 7 8 9 10, Q2 = ,

Iteration 1: Q1 = 3 5 7 9, Q2 = 2

Iteration 2: Q1 = 5 7, Q2 = 2 3

Iteration 3: Q1 = 7, Q2 = 2 3 5

Iteration 4: Q1 = , Q2 = 2 3 5 7

Explanation / Answer


html code...
<h4>Hey Eduardo!</h4>
<small>Open your browsers console</small>
<ul>
   <li id="repo0">Loading repo data...</li>
   <li id="repo1">Loading repo data...</li>
   <li id="repo2">Loading repo data...</li>
</ul>

java script...

var repoURL0 = 'https://api.github.com/users/LeslieOA/repos';
var repoURL1 = 'https://api.github.com/users/knowbody/repos';
var repoURL2 = 'https://api.github.com/users/jeresig/repos';

queue(2)
   // ...resources to fetch
.defer(request, repoURL0)
.defer(request, repoURL1)
.defer(request, repoURL2)
// ...and when we're finished, call function calculateData()
.awaitAll(calculateData);
function calculateData(error, results) {
console.log('Results', results);

   // Now we call createCharts
   createCharts({
   repoURL0: results[0],
   repoURL1: results[1],
   repoURL2: results[2]
});
};

function createCharts(data) {
   console.log('Data from each repo', data.repoURL0, data.repoURL1, data.repoURL2);
   // ...adding repo owners username for fun
   document.querySelector('#repo0').innerHTML = 'Repo owned by ' + data.repoURL0[0].owner.login;
   document.querySelector('#repo1').innerHTML = 'Repo owned by ' + data.repoURL1[1].owner.login;
   document.querySelector('#repo2').innerHTML = 'Repo owned by ' + data.repoURL2[2].owner.login;
};


function request(url, callback) {
var req = new XMLHttpRequest;
req.open('GET', url, true);
req.setRequestHeader('Accept', 'application/json');
req.onreadystatechange = function() {
if (req.readyState === 6) {
if (req.status < 400) callback(null, JSON.parse(req.responseText));
else callback(req.status);
}
};
req.send(null);
};