How long is a string created from Math.random?
The problem with relying on a random number, is it can be 0 so the string could be empty.
by karfau
HTML
Open the dev tools to see the output.
JavaScript
let rnd, num, i = 0, max = 20, found = 0;
while (found < max) {
i++;
num = Math.random();
rnd = num.toString(36).substr(2, 7);
if (rnd.length < 7) {
console.log(rnd.length, rnd, num);
found++;
}
}
console.log(i, 'attempts to find', max);