Is there some way to make random strings with .repeat()
still random? If I use this:
JavaScript
x
2
1
console.log(`${Math.random()} | `.repeat(5));
2
the output is something like this:
JavaScript
1
2
1
0.2564646392254777 | 0.2564646392254777 | 0.2564646392254777 | 0.2564646392254777 | 0.2564646392254777 |
2
In a nutshell, the output is the same.
Advertisement
Answer
What your code currently does is:
- Generate a random number within a string
- Repeat
n
times that string.
What you want is generate n
random number strings, then join them.
Here is a function that does this:
JavaScript
1
4
1
function randomNumberString(n) {
2
return Array(n).fill(0).map(_ => `${Math.random()}`).join(' | ');
3
}
4
console.log(randomNumberString(10));
And if you really want the |
at the end:
JavaScript
1
4
1
function randomNumberString(n) {
2
return Array(n).fill(0).map(_ => `${Math.random()} | `).join('');
3
}
4
console.log(randomNumberString(10));