Home > Software engineering >  Line break in javascript after random numbers
Line break in javascript after random numbers

Time:10-23

I know that is very simple, but why is the line break not working here and the numbers keep printing in a single line? I want to print 100 random numbers on a line and then again 100 random numbers, but on a new line.

function generateNumber() {

  const arr = Array(100)

  for (i = 0; i < arr.length; i  ) {

    setInterval(() => {
      let randomNumber = (Math.floor(Math.random() * 10)   1);
      document.write(randomNumber   "\n")
    }, 3000);

  }
}

function test() {
  generateNumber();
}

test();

CodePudding user response:

Try putting <br> instead of \n.

Edit: I just saw your comment explaining you want 100 digits on each line. Try this:

function generateNumber() {
    for (var i = 0; i < 100; i  ) {
        document.write(Math.floor(Math.random() * 10)   1);
    }
    document.write("<br>");
}

function test() {
  generateNumber();
}

test();
setInterval(test, 3000);

Second edit: it seems from your code you want this to run every three seconds, so I have added the setInterval line to the end above. If you don't want that, remove that line.

CodePudding user response:

Maybe this is what you actually want?

setInterval(() => {
  for (let i = 0; i < 100; i  )
    document.write(Math.floor(Math.random() * 10));
  document.write("<br>");
}, 1000);

  • Related