2

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();
3limin4t0r
  • 19,353
  • 2
  • 31
  • 52
Michael V
  • 45
  • 6
  • 2
    You need to use `
    ` instead of `\n` to add a line break in HTML
    – Nick Parsons Oct 22 '22 at 10:12
  • i'm sorry, but if i put
    it will print me into a column 100 numbers, i wanna like this one: 22323232323 /n 4650569568
    – Michael V Oct 22 '22 at 10:20
  • 1
    So what is the criteria for the line break? I first thought that a line break should happen each 10 digits, but that might not be the case since the first number is 11 digits and the second number is 10 digits. It might be a typo and the first number should also be 10 digits. – 3limin4t0r Oct 22 '22 at 10:29
  • @MichaelV should each row also have 100 numbers? So in total, 100 lines, with each line containing 100 numbers? Where each line is generated 3s apart from each other? Is that what you're trying to do? – Nick Parsons Oct 22 '22 at 10:32
  • @NickParsons yep, - > after 100 random number a new line with a new 100 random numbers – Michael V Oct 22 '22 at 10:36
  • @MichaelV and how many lines total? 100? Or infinite...? – Nick Parsons Oct 22 '22 at 10:48
  • doesn't matter i think – Michael V Oct 22 '22 at 10:52

2 Answers2

3

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.

ORGPEV
  • 166
  • 6
2

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);
3limin4t0r
  • 19,353
  • 2
  • 31
  • 52
Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43