0

Is there some way to make random strings with .repeat() still random? If I use this:

console.log(`${Math.random()} | `.repeat(5)); 

the output is something like this:

0.2564646392254777 | 0.2564646392254777 | 0.2564646392254777 | 0.2564646392254777 | 0.2564646392254777 | 

In a nutshell, the output is the same.

1
  • Do you really need the | at the end ? Commented Dec 26, 2021 at 1:20

1 Answer 1

5

What your code currently does is:

  1. Generate a random number within a string
  2. Repeat n times that string.

What you want is generate n random number strings, then join them.

Here is a function that does this:

function randomNumberString(n) { return Array(n).fill(0).map(_ => `${Math.random()}`).join(' | '); } console.log(randomNumberString(10));

And if you really want the | at the end:

function randomNumberString(n) { return Array(n).fill(0).map(_ => `${Math.random()} | `).join(''); } console.log(randomNumberString(10));

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.