mercredi 1 septembre 2021

Without changing the code below .Print the numbers from 1 to 100 in order ,The entire process should be completed within 20 second

Executing this code will print the numbers in a random order. The Task is to ( 1 )Print the numbers from 1 to 100 in order ( 2 )The entire process should be completed within 20 seconds

// DO NOT CHANGE

const startTime = Date.now();

const getRandomNumber = (start, end) => {
  return Math.random() * end + start;
};

const printNumber = (i, next) => {
  const delay = getRandomNumber(1, 10) * 1000;
  setTimeout(() => {
    next(`Number ${i}`);
  }, delay);
};

function randomInteger(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

// YOUR CODE HERE

for (let i = 0; i < 100; i++) {
  printNumber(i, (text) => {
    console.log(`${Math.ceil((Date.now() - startTime) / 1000)} - ${text}`);
  });
}



Aucun commentaire:

Enregistrer un commentaire